非法请求。请求格式错误或无效。从Azure获取访问令牌时

时间:2019-07-03 13:04:10

标签: azure oauth-2.0 azure-active-directory azure-ad-graph-api

我正在尝试从Azure AD获取访问令牌。为此,我在以下步骤中执行了

1)在Azure Active Directory上创建企业应用程序

enter image description here

2)我可以使用授权获得访问令牌--- >>“获取新访问令牌”

enter image description here

3)点击https://login.microsoftonline.com/<Application ID>/oauth2/token URL的“ POST”请求

已在下面为POST正文配置

  • 授权类型为OAuth 2.0
  • 使用“表单数据”通过身体,如下所示 enter image description here

ClientID是第一步中创建的应用程序中的ApplicationID

代码也是第一步创建的应用程序中的ApplicationID

不确定我是否已正确配置

4)当我尝试发送低于错误响应的请求时,使用相同的访问码

`{
    "error": "invalid_grant",
    "error_description": "AADSTS9002313: Invalid request. Request is malformed or invalid.\r\nTrace ID: 60b8fb68-40d5-43da-9b7b-36de021c2900\r\nCorrelation ID: 90ed2f2c-1ac8-4044-8742-493a3fce51be\r\nTimestamp: 2019-07-03 12:42:32Z",
    "error_codes": [
        9002313
    ],
    "timestamp": "2019-07-03 12:42:32Z",
    "trace_id": "60b8fb68-40d5-43da-9b7b-36de021c2900",
    "correlation_id": "90ed2f2c-1ac8-4044-8742-493a3fce51be"
}

enter image description here

请让我知道我错了或需要更改的地方。

2 个答案:

答案 0 :(得分:3)

如果我没记错,您正在尝试使用OAuth 2.0 code grant flow获取令牌。

此代码流有两个步骤:

  1. 请求授权码
  2. 使用此授权码需要请求令牌

获取授权码

您将以下代码粘贴到浏览器中或发布人中。在邮递员中,如下所示:

enter image description here

https://login.microsoftonline.com/YourTennatId.onmicrosoft.com/oauth2/authorize?client_id=YourClentId&response_type=code&redirect_uri=https://www.getpostman.com/oauth2/callback&response_mode=query&scope=offline_access%20user.read%20mail.read

获得授权码后,将其复制以进行下一步。

使用授权码请求访问令牌:

Token Request Endpoint: https://login.microsoftonline.com/YourTenantId/oauth2/token

client_id:YourClientId
scope:https://graph.microsoft.com/User.ReadWrite.All
redirect_uri:https://www.getpostman.com/oauth2/callback
grant_type:authorization_code
client_secret:YourAppsSecret
code:Paste Your Code Here

邮递员格式:

enter image description here

希望这可以解决您的问题。

答案 1 :(得分:1)

我遇到了同样的问题,最后发现我的问题是重定向uri授权后返回的代码实际上类似于www.yourredirecturl.com/?code=....&section_state=....

因此,在?code=之后简单地复制整个内容,包括&section_state,并使code不正确。希望这能解决您的问题。

相关问题