我正在開發一個使用 microsoft oauth2 (On-Behalf-Of-User Flow) 的 Angular Flask 應用程式我試圖從后端呼叫一個 api,但在這里我得到一個例外。
角度:
- 這是app.module.ts中的配置:
export function MSALInstanceFactory(): IPublicClientApplication {
return new PublicClientApplication({
auth: {
clientId: '<application_id_of_spa>',
authority: 'https://login.microsoftonline.com/organizations',
redirectUri: 'http://localhost:4200/'
},
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
storeAuthStateInCookie: isIE,
},
system: {
loggerOptions: {
loggerCallback,
logLevel: LogLevel.Info,
piiLoggingEnabled: false
}
}
});
}
export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
const protectedResourceMap = new Map<string, Array<string>>();
protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']);
protectedResourceMap.set('https://api.powerbi.com/v1.0/myorg/', ['https://analysis.windows.net/powerbi/api/.default']);
protectedResourceMap.set('http://localhost:5000/api/v1.0/get_workspaces',['api://<application_id_of_webapi>/.default'])
return {
interactionType: InteractionType.Popup,
protectedResourceMap
};
}
export function MSALGuardConfigFactory(): MsalGuardConfiguration {
return {
interactionType: InteractionType.Popup,
authRequest: {
scopes: ['api://<application_id_of_webapi>/.default'],
},
};
}
然后我使用“acquireTokenPopup”msal 函式來獲取訪問令牌
然后我像這樣呼叫我的后端api
this.http.get('http://localhost:5000/api/v1.0/get_workspaces')在我的 Flask Web API 上:
@app.route('/api/v1.0/get_workspaces', methods=['GET'])
def get():
current_access_token = request.headers.get("Authorization", None)
msal_client = msal.ConfidentialClientApplication(
client_id=app.config['CLIENT_ID'],
authority=app.config['AUTHORITY'],
client_credential=app.config['CLIENT_SECRET'])
# acquire token on behalf of the user that called this API
arm_resource_access_token = msal_client.acquire_token_on_behalf_of(
user_assertion=current_access_token.split(' ')[1],
scopes=app.config['SCOPE']
)
print( arm_resource_access_token) /////////////////// ******* I'm getting the error here
headers = {
'Authorization': arm_resource_access_token['token_type'] ' ' arm_resource_access_token['access_token']}
workspaces= requests.get(app.config['ENDPOINT'] 'workspaces', headers = headers).json()
print(workspaces)
return jsonify(workspaces)
在我的角度控制臺中,我得到了這個:[1]:https ://i.stack.imgur.com/LMXNa.png
在我的 Flask 終端中,我得到了這個:
AADSTS65001: The user or administrator has not consented to use the application with ID <webapi_ app_id>.在 azure 門戶中,我注冊了 spa 和 web api:- webapi 權限:[2]:https ://i.stack.imgur.com/4Wfg4.png
-- 我在后端公開了 api 并將其添加到我的前端注冊中。- 我在授權客戶端應用程式上添加了我的 spa app_id。
uj5u.com熱心網友回復:
AADSTS65001:用戶或管理員未同意使用該應用程式
當您在檢索訪問令牌時錯過授予管理員對添加范圍的同意時,通常會發生此錯誤。
要解決此錯誤,請檢查您是否暴露了如下 API:
轉到 Azure 門戶 -> Azure Active Directory -> 應用注冊 -> 您的應用 -> 公開 API

公開 API 后,請確保授予API permissions它,如下所示:
轉到 Azure 門戶 -> Azure Active Directory -> 應用注冊 -> 您的應用 -> API 權限 -> 添加權限 -> 我的 API -> 您的 API

添加 API 權限后,如果需要,請確保授予管理員同意。
當您嘗試獲取訪問令牌時,請檢查您是否啟用了以下選項:
轉到 Azure 門戶 -> Azure Active Directory -> 應用注冊 -> 您的應用 -> 身份驗證

如果錯誤仍然存??在,請檢查以下鏈接:
azure 活動目錄 - InteractionRequiredAuthError:AADSTS65001:用戶或管理員未同意使用 ID 的應用程式 - VoidCC
.net - “AADSTS65001:用戶或管理員未同意使用該應用程式”在代表用戶獲取令牌時發生 - Thinbug
更新:
正如您在評論中提到的,請確保將您的客戶端應用程式添加到已知的客戶端應用程式串列中
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/483705.html
標籤:有角度的 烧瓶 oauth-2.0 天蓝色活动目录 msal
上一篇:相當于axios(nodejs)中用于檔案上傳的curl命令?
下一篇:AttributeError:'list'物件沒有屬性'_sa_instance_state'Traceback(最近一次呼叫最后)
