【发布时间】: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/…
-
查看我的回答更新,如有任何问题,请随时给我打电话。