【问题标题】:How do I get an access token for Microsoft Graph -- Azure docs are incorrect如何获取 Microsoft Graph 的访问令牌——Azure 文档不正确
【发布时间】:2023-02-24 13:01:46
【问题描述】:

我正在尝试使用 Step 4 之后的 OAuth 2.0 客户端凭据授权流程获取访问令牌。这是我的卷发

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d '{"grant_type":"client_credentials","client_id":"my_client_id", "client_secret":"my_client_secret", "scope":"https://graph.windows.com/.default"}' https://login.microsoftonline.com/my_tenant_id/oauth2/v2.0/token

它给出了这个错误消息,这显然是错误的,因为我的请求正文包括grant_type

{"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: a95260ff-63b6-405f-880b-738bfda33b00\r\nCorrelation ID: d606ab93-59c7-4d7d-ac45-643074e23a75\r\nTimestamp: 2023-02-24 02:29:25Z","error_codes":[900144],"timestamp":"2023-02-24 02:29:25Z","trace_id":"a95260ff-63b6-405f-880b-738bfda33b00","correlation_id":"d606ab93-59c7-4d7d-ac45-643074e23a75","error_uri":"https://login.microsoftonline.com/error?code=900144"}

如何获得访问令牌以使用http://graph.windows.net 查找已注册的应用程序?

【问题讨论】:

    标签: azure oauth-2.0 clientcredential


    【解决方案1】:

    我试图在我的环境中重现相同的结果并得到以下结果:

    我注册了一个 Azure AD 应用程序并添加了API权限如下所示:

    运行相同卷曲命令通过邮递员,我点击了导入并粘贴了如下代码:

    curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d '{"grant_type":"client_credentials","client_id":"678b1771-0703-401e-8056-xxxxxxxxxx", "client_secret":"xxxxxxxxxxxxxxxx", "scope":"https://graph.windows.com/.default"}' https://login.microsoftonline.com/58e70374-11f2-4e91-af40-xxxxxxxxxxx/oauth2/v2.0/token
    

    选择后继续,它把我带到了下一个屏幕,如下所示:

    当我点击Import时,我得到了以下参数的屏幕同样的错误选择像这样发送后:

    您收到该错误是因为您没有传递参数正确的格式。

    要解决该错误,请尝试更改您的卷曲命令通过以以下格式传递由 & 分隔的参数:

    curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=client_credentials&client_id=<apID>&client_secret=<secret>&scope=https%3A%2F%2Fgraph.windows.com%2F.default' https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    

    我改变了卷曲命令通过以上述格式传递参数并像这样再次导入它:

    当我点击导入时,参数正确传入Body部分但得到了不同的错误像这样:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    grant_type:client_credentials
    client_id:<appID>
    client_secret: <secret>
    scope: https://graph.windows.com/.default
    

    回复:

    解决上面的错误,我改了scopehttps://graph.microsoft.com/.default并得到访问令牌成功是这样的:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    grant_type:client_credentials
    client_id:<appID>
    client_secret: <secret>
    scope: https://graph.microsoft.com/.default
    

    回复:

    alure

    在你的情况下,你需要改变你的卷曲命令通过以正确的格式传递参数,由&amp;范围值也像这样:

    curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=client_credentials&client_id=<appID>&client_secret=<secret>&scope=https://graph.microsoft.com/.default' https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2017-08-31
      • 2016-10-29
      • 2019-08-05
      • 1970-01-01
      • 2018-11-09
      相关资源
      最近更新 更多