【问题标题】:"The access grant authorization_code is not supported" from Azure AD using Oauth 2使用 Oauth 2 的 Azure AD 中的“不支持访问授权授权码”
【发布时间】:2014-10-14 01:03:40
【问题描述】:

我正在使用 Azure AD 处理 Authorization Code Grant Flow。尽管文档说 grant_type 应该是 authorization_code,但我收到了有关此属性的错误消息。

POST https://login.windows.net/SOME_AZURE_AD_UUID/oauth2/token?api-version=1.0
Content-Type: application/x-www-form-urlencoded

client_id=SECRET_CLIENT_ID
&client_secret=SECRET_CLIENT_SECRET
&code=SECRET_CODE
&grant_type=authorization_code
&redirect_uri=https://myserver.example.com/login/auth_return
&resource=https://myserver.example.com/
&scope=openid email

编辑:为清楚起见添加了空格)

我回来的错误:

HTTP/1.1 400 Bad request
Content-Length: 436
X-Content-Type-Options: nosniff
X-Powered-By: ASP.NET
Request-Id: SOME_REQUEST_ID
X-Ms-Request-Id: SOME_REQUEST_ID
Strict-Transport-Security: max-age=31536000; includeSubDomains
Set-Cookie: x-ms-gateway-slice=slicea; path=/; secure; HttpOnly, stsservicecookie=acs; path=/; secure; HttpOnly
Server: Microsoft-IIS/8.0
Cache-Control: private
Date: Wed, 20 Aug 2014 14:44:08 GMT
Content-Type: application/json; charset=utf-8

{
  "correlation_id": "SOME_CORRELATION_ID",
  "error": "unsupported_grant_type",
  "error_codes": [
    70003
  ],
  "error_description": "
    ACS70003: The access grant 'authorization_code' is not supported.\r\n
    Trace ID: SOME_TRACE_UUID\r\n
    Correlation ID: SOME_CORRELATION_ID\r\n
    Timestamp: 2014-08-20 14:44:08Z",
  "timestamp": "2014-08-20 14:44:08Z",
  "trace_id": "SOME_TRACE_UUID"
}

(为清楚起见添加了空格)

如果我将grant_type 更改为client_credentials,此请求确实有效(但我还没有找到一种方法来使用生成的令牌来满足我的需要)。如果我将一些 URL 更改为指向 Google 而不是 Azure AD,它也可以工作。

这些请求是否存在错误,或者服务是否真的不支持记录在案的grant_typeauthorization_code

【问题讨论】:

    标签: azure active-directory oauth-2.0


    【解决方案1】:

    我相信这是一个错误,我花了 2-3 天时间才弄明白。请执行以下操作以使其正常工作,

    1) 从您的 URL 中删除“?api-version=1.0”。我知道这听起来很奇怪,但相信我,他们的文档一团糟。

    2) 在您的请求中添加一个 "Content-Type": "application/x-www-form-urlencoded" 标头(因此您必须对发布数据值进行编码......例如 redirect_url=(encodedURL)等等

    3)从帖子数据中删除不必要的字段 REFER ...应该是这样的

    {
        'grant_type': "authorization_code",
        'resource': "your resource",
        'client_id': "your client Id",
        'redirect_uri': "your redirect URL",
        'client_secret': "your client secret",
        'code': "the code u got"
    }
    

    我看到你已经完成了第 2 点,所以你需要完成第 1 点,然后你就可以开始了。

    此外,如果您想快速获得 access_token(如果我所说的对您没有任何帮助),然后在 grant_type 中传递“client_credentials”,您将获得一个较小的 access_token 响应。但是,如果您还想要使用 refresh_token 的完整响应,则必须执行所有这些步骤。

    编辑: 他们的文档中还有一个错误,对于 Refresh Tokens >>> URL 应该是 oauth2/token 而不是 oauth2/authorize

    希望这会有所帮助!

    【讨论】:

    • 对不起,我弄糊涂了。我总是将它发布为 URLencoded,但我的调试语句将它打印为 JSON。我已更新问题以更清楚地反映发布的内容。
    • 所以我 (1) 从 URL 中删除了 api-version 并 (2) 从正文中删除了 scope。现在我收到一条新的错误消息,这听起来像是进步:)
    • 实际上,我想删除(可选)resource 并保留 scope 以便它返回电子邮件地址。我接受了你的回答,因为它让我走上了正确的道路,但我建议你更新它,因为我真的想要那个电子邮件地址字段。
    • @seyed-mauze-rehan ...顺便说一句:非常感谢!
    • 为什么不使用 Oauth“状态”字段呢?我的意思是你想取回我想的电子邮件ID......使用oauth状态字段(协议标准,也推荐用于此调用)?此外,“资源”非常重要,因为它告诉您正在尝试授权哪个站点。假设您给了“graph.windows.net”,然后您想访问您的 Sharepoint……说“blabla-my.sharepoint.com”,然后您会收到一条错误消息。
    【解决方案2】:

    试试这个

    'grant_type':"client_credentials",
    'resource': "your resource",
    'client_id': "your client Id",
    'redirect_uri': "your redirect URL",
    'client_secret': "your client secret",
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多