【问题标题】:Microsoft Graph API returning 404 error missing parameter, despite grant_type in query stringMicrosoft Graph API 返回 404 错误缺少参数,尽管查询字符串中有 grant_type
【发布时间】:2019-01-22 08:53:30
【问题描述】:

通过本教程的第 4 部分调用 Microsoft Graph API 以获取访问令牌:https://docs.microsoft.com/en-us/graph/auth-v2-service?view=graph-rest-1.0

我正在执行以下获取请求:

  var options = {
    "method": "POST",
    "contentType": "application/x-www-form-urlencoded",
    "accept": 'application/json',
    "headers": {
      'Content-Type': "application/x-www-form-urlencoded",
    },
  }
  var authorization_url = "https://login.microsoftonline.com/" + tenant + 
    "/oauth2/v2.0/token/" + 
      "client_id=" + client_id + 
        "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default" +
          "&client_secret=" + client_secret + 
              "&grant_type=client_credentials";

我确信我的所有参数都是正确的,但它返回一个无效的请求错误:“请求正文必须包含以下参数:'grant_type'”。知道可能出了什么问题吗?

【问题讨论】:

  • 我不知道 API,但错误消息具体指的是 body,而您在 URI 查询部分而不是请求正文中放置了一个标志.

标签: javascript microsoft-graph-api


【解决方案1】:

您的authorization_url 应该只包含端点(即没有查询字符串):

var authorization_url = "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/token/"

参数属于POST的正文:

var payload = 
    "client_id=" + client_id + 
    "&client_secret=" + client_secret + 
    "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default" +
    "&grant_type=client_credentials";

(你的options 我觉得不错)

【讨论】:

    【解决方案2】:

    我怀疑您指的是that page 上的token request 部分。

    它显示了 HTTP 请求的以下表示:

    // Line breaks are for legibility only.
    
    POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token HTTP/1.1
    Host: login.microsoftonline.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=535fb089-9ff3-47b6-9bfb-4f1264799865
    &scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
    &client_secret=qWgdYAmab0YSkuL1qKv5bPX
    &grant_type=client_credentials
    

    最低部分表示 HTTP 请求的正文,而不是 URL 查询字符串中传递的参数。

    我建议您更改代码以适应这些更改。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2014-10-14
      • 2012-06-27
      相关资源
      最近更新 更多