【问题标题】:AADSTS65001 invalid_grant when all permissions have admin consentAADSTS65001 invalid_grant 当所有权限都得到管理员同意时
【发布时间】:2021-04-15 02:04:07
【问题描述】:

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

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'

【问题讨论】:

  • 如果发布的答案解决了您的问题,请单击复选标记将其标记为答案。这样做可以帮助其他人找到他们问题的答案。见:meta.stackexchange.com/questions/5234/…
  • 查看我的回答更新,如有任何问题,请随时给我打电话。

标签: azure microsoft-graph-api


【解决方案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获取access token,需要登录用户获取授权码,然后使用授权码兑换access token。

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端点来获取令牌,这本身并没有风险,你可以放心使用。

【讨论】:

  • 非常感谢您的详尽回答,但我以为我使用的是 microsoft graph,实际上我直接从文档中获得了 API 请求。您是否注意到因为 login.windows.net 而使用了已弃用的 Azure AD 图?还是别的什么?我将尝试您的建议,并在可行后返回此处选择您的答案作为答案。再次感谢。
  • 感谢您的回答,但不幸的是它对我不起作用,由于不同意,它仍然给我同样的错误授权无效:/
  • @HamzehMuaz 你是调用 Azure Active Directory Graph api https://graph.windows.net/ 还是 Microsoft Graph api https://graph.microsoft.com/
  • @HamzehMuaz AAD 图:i.stack.imgur.com/UbxDs.png。微软图:i.stack.imgur.com/iumgv.png
  • @HamzehMuaz 您是否运行了管理员同意网址?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多