AADSTS65001 invalid_grant 当所有权限都获得管理员同意时

时间:2021-04-15 02:04:07

标签: azure microsoft-graph-api

我正在尝试代表用户为我的应用获取访问令牌和刷新令牌。 我正在使用以下代码:

curl --location --request POST 'https://login.windows.net/common/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=my-app-id' \
--data-urlencode 'scope=user.read,user.readbasic.all,files.read,files.read.all,files.read.selected,files.readwrite,files.readwrite.all,mail.read,mail.read.shared,mail.readbasic,mail.readwrite,mail.send' \
--data-urlencode 'code=my-code' \
--data-urlencode 'redirect_uri=http://localhost:4200/callback' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_secret=client-secret'

暂时使用 POSTMAN。

我收到以下错误响应:

{
    "error": "invalid_grant",
    "error_description": "AADSTS65001: The user or administrator has not consented to use the application with ID 'my-app-id' named 'testapp'. Send an interactive authorization request for this user and resource.\r\nTrace ID: 86781b44-3989-4b54-b905-697beba50400\r\nCorrelation ID: bbe88855-3d61-4f13-85c1-05e48245e1a1\r\nTimestamp: 2021-04-15 01:40:45Z",
    "error_codes": [
        65001
    ],
    "timestamp": "2021-04-15 01:40:45Z",
    "trace_id": "86781b44-3989-4b54-b905-697beba50400",
    "correlation_id": "bbe88855-3d61-4f13-85c1-05e48245e1a1",
    "suberror": "consent_required",
    "claims": "{\"access_token\":{\"capolids\":{\"essential\":true,\"values\":[\"809ad5c7-1cbd-4502-a52f-f749fa4b5049\"]}}}"
}

我在范围内使用的所有 API 权限均已获得管理员同意。

我相信这是一个错误,因为以前我只使用“User.Read,offline_access”范围时遇到了同样的错误,这两个范围都得到了管理员的同意。但是,删除“offline_access”范围后一切正常,我能够检索刷新令牌。

但是现在,错误又回来了,我不确定可能是什么问题。

我尝试检查参数并再次给予管理员同意,但没有任何效果。

如果您知道我应该做什么,请告知。

谢谢。

Ms Graph permissions consent Image

编辑:从 url 中删除“v2.0”,将其保留为“https://login.windows.net/common/oauth2/token”已经有效..但我不确定这是否正确做?有什么风险吗?

答案: 范围用逗号分隔,它们应该用空格分隔。这是有效的 cURL:

curl --location --request POST 
'https://login.microsoftonline.com/common/oauth2/v2.0/token' 
--data-urlencode 'client_id=xx-x-xx-x' \
--data-urlencode 'scope=user.read user.readbasic.all files.read files.read.all files.read.selected files.readwrite files.readwrite.all mail.read mail.read.shared mail.readbasic mail.readwrite mail.readwrite.shared mail.send mail.send.shared' \
--data-urlencode 'code=CODEHERE' \
--data-urlencode 'redirect_uri=http://localhost:4200/callback' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_secret=SECRET'

1 个答案:

答案 0 :(得分:1)

您正在使用已弃用的 Azure AD 图表。建议您使用最新的microsoft graph

首先,在浏览器中运行管理员同意 URL。这将在租户范围内授予应用程序管理员的同意。 https://login.microsoftonline.com/{tenant id}/adminconsent?client_id={client id}&state=12345&redirect_uri={redirect_uri}

接下来需要使用auth code flow获取访问令牌,需要先登录用户获取授权码,然后使用授权码兑换访问令牌。

>

1.在浏览器中请求授权码。

https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/authorize?
client_id={client app client id}
&response_type=code
&redirect_uri={redirect_uri}
&response_mode=query
&scope=https://graph.microsoft.com/.default
&state=12345

2.兑换代币。

https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/token 

client_id={client id}
&scope=openid offline_access https://graph.microsoft.com/.default
&code={code}
&redirect_uri={redirect_uri}
&grant_type=authorization_code
&client_secret={client_secret}

添加:

顺便说一句,我只是建议您使用最新版本。当然,如果你使用旧版本:https://login.windows.net,也没有问题。

另外,如果你从url中删除v2.0,只是意味着你将使用v1.0端点来获取令牌,这本身没有风险,你可以放心使用。