我正在开发一个使用微软OAuth2 (代表用户流)的角形+水瓶应用程序。我试图从后端调用一个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')我的水瓶网络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)在我的角度控制台里,我得到了这个:

在我的酒瓶终端里,我得到了这个:
AADSTS65001: The user or administrator has not consented to use the application with ID <webapi_ app_id>.在Azure门户网站上,我注册了spa和web:

我在后端公开了API,并将其添加到前端注册中。
我在授权的客户应用程序中添加了我的spa app_id。
发布于 2022-05-29 01:22:19
AADSTS65001:用户或管理员尚未同意使用应用程序
当您在检索访问令牌时没有向添加的范围授予管理同意书时,通常会发生此错误。
若要解决此错误,请检查是否公开了如下所示的API:
转到Azure Portal -> Azure Active Directory ->应用程序注册->公开API

在公开API之后,请确保为其授予API permissions,如下所示:
转到Azure Portal -> Azure Active Directory ->应用程序注册-> API权限->添加权限-> My ->您的API

转到Azure Portal -> Azure Active Directory ->应用程序注册->身份验证

如果错误仍然存在,请检查下面的链接:
更新:
正如您在注释中提到的,请确保将客户端应用程序添加到已知客户端应用程序列表中。
https://stackoverflow.com/questions/72419373
复制相似问题