我正在尝试在我的 SPA 应用程序中实现 Azure SSO。因此,我能够对用户进行身份验证,并作为响应,我取回访问 token 。
之后,我尝试通过此网址获取用户基本配置文件:
https://graph.microsoft.com/v1.0/me
授权为不记名 token 。但由于某种原因,我收到以下错误响应
{
"error": {
"code": "UnknownError",
"message": "",
"innerError": {
"date": "2023-08-31T09:55:43",
"request-id": "eaf36ab5-aba3b1",
"client-request-id": "eaf36ab5-aba3-49b1"
}
}
}
我不知道出了什么问题。 azure上的应用程序设置有问题吗? 请检查我当前的设置:
谢谢。
最佳答案
The error occurred as your token don't have User.Read permission in
scp
claim that is mandatory for reading user info.
我注册了一个 Azure AD 应用程序并添加了 User.Read
权限,如下所示:
现在,我通过 Postman 使用授权代码流生成 token ,参数如下:
POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id: <app_ID>
grant_type:authorization_code
scope: openid profile
code: code
redirect_uri: https://jwt.ms
client_secret: <secret>
回应:
当我使用此 token 获取用户信息时,我也遇到了相同的错误,如下所示:
GET https://graph.microsoft.com/v1.0/me
回应:
To resolve the error, make sure to generate access token with
User.Read
scope.
当我在浏览器中使用 User.Read
范围运行授权请求时,出现了以下同意屏幕:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
client_id=appId
&redirect_uri=https://jwt.ms
&response_type=code
&response_mode=query
&scope= openid profile User.Read
&state=12345
同意上述提示后,我在地址栏成功获取了授权码:
现在,我使用此代码生成具有 User.Read
范围的访问 token ,并获得如下 token :
POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id: <app_ID>
grant_type:authorization_code
scope: openid profile
code: code
redirect_uri: https://jwt.ms
client_secret: <secret>
回应:
当我使用此 token 获取用户信息时,我成功获得了响应,如下所示:
GET https://graph.microsoft.com/v1.0/me
回应:
就您而言,请确保在 scope 参数中传递 User.Read
。
关于Azure SSO 无法通过访问 token 获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77014771/