我收到错误“访问 token 验证失败。受众无效。” 对于应用程序,将 api 权限设置为offline_access、openid、profile、User.Read。 用户启动身份验证,转到 MS 身份验证站点,询问登录名、密码和等级。
交换代码以访问 token 后,我很好地收到了
{'token_type': 'Bearer', 'scope': 'offline_access openid profile User.Read', 'expires_in': '3906', 'ext_expires_in': '3906', 'expires_on': '1653988700', 'not_before': '1653984493', 'resource': 'my_azure_client_id', ....}
然后我尝试使用此访问 token 获取当前用户的配置文件。 结果我收到错误“访问 token 验证失败。受众无效。”
帮助豌 bean )
最佳答案
出现该错误的原因是您的 token 有错误的受众。
请检查您使用什么 token 来调用Graph API。
我尝试在我的环境中重现相同的内容。
如果您使用 ID Token
而不是 Access Token
,您可能会收到如下错误:
要了解您提供的是访问 token 还是ID token ,请在JSON Web Tokens - jwt.io中对其进行解码.
对于access token ,aud
声明将为 "https://graph.microsoft.com"
或 "00000003-0000-0000-c000- 000000000000"
对于id token ,aud
声明将为 “your_app_client_id”
仔细选择访问 token ,aud
为“https://graph.microsoft.com”
调用 Microsoft Graph API 时:
获取profile for current user ,您可以使用以下查询:
GET https://graph.microsoft.com/v1.0/me
我使用访问 token 成功获取了配置文件,如下所示:
在生成访问 token 时将范围替换为 https://graph.microsoft.com/.default
以避免混淆。
引用:
oauth 2.0 - Microsoft Graph API: Access token validation failure. Invalid audience - Stack Overflow
更新:
为了获取授权代码,请通过更改范围来发出请求,如下所示:
https://login.microsoftonline.com/your_tenant_id/oauth2/v2.0/authorize?
client_id=your_client_id
&response_type=code
&redirect_uri=xxxxxx
&response_mode=query
&scope=https://graph.microsoft.com/.default
&state=12345
关于Azure AD 我的个人资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72444516/