Microsoft graph API:无法使用生成的访问令牌获取用户

时间:2019-06-11 11:52:53

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

我已经在Azure门户中注册了该应用程序,并且生成了client_secret。 我需要客户端授予流程,并且我还具有应用程序权限。我也已经获得了管理员的同意,就像我本人一样。

我能够使用给定的URL生成访问令牌:

https://login.microsoftonline.com/47be0abf-c6a1-4f04-a665-dceb081c4ff1/oauth2/v2.0/token?client_id=********&client_secret=******&grant_type=client_credentials&scope=User.ReadBasic.All%20User.Read%20User.ReadWrite%20User.Read.All%20User.ReadWrite.All%20Directory.Read.All%20Directory.ReadWrite.All%20Directory.AccessAsUser.All

但是,当我使用生成的令牌访问以下URL时,会收到权限不足消息。

https://graph.microsoft.com/v1.0/users
Authorization Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFEQ29NcGpKWHJ4VHE5Vkc5dGUtN0ZYNndkRlV3aTBKbGlHcWhEWkgybFRlYWh6SUhUX0VsazFaYTFuUHRzNWo3SW5xMDBmbnNNRkpNUWRYdWdVZnpaZ0cxT19uenNPTXpwN2tpUFFIR2VHTnlBQSIsImFsZyI6IlJTMjU2IiwieDV0IjoiQ3RmUUM4TGUtOE5zQzdvQzJ6UWtacGNyZk9jIiwia2lkIjoiQ3RmUUM4TGUtOE5zQzdvQzJ6UWtacGNyZk9jIn0.eyJhdWQiOiJodHRwczovL2dyYXBoLm1pY3Jvc29mdC5jb20iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC80N2JlMGFiZi1jNmExLTRmMDQtYTY2NS1kY2ViMDgxYzRmZjEvIiwiaWF0IjoxNTYwMjUzMDE1LCJuYmYiOjE1NjAyNTMwMTUsImV4cCI6MTU2MDI1NjkxNSwiYWlvIjoiNDJaZ1lQajhVdnBwWGMySEU1WGZwbnZxSG43akFnQT0iLCJhcHBfZGlzcGxheW5hbWUiOiJUdXRvcmlhbCBTYW1wbGUgQXBwIiwiYXBwaWQiOiI2NzMxZGU3Ni0xNGE2LTQ5YWUtOTdiYy02ZWJhNjkxNDM5MWUiLCJhcHBpZGFjciI6IjEiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC80N2JlMGFiZi1jNmExLTRmMDQtYTY2NS1kY2ViMDgxYzRmZjEvIiwib2lkIjoiNjg0ZjkzMjUtNjUyNS00Yjk5LTgwNzktOTEyOGZjZWNlNGViIiwic3ViIjoiNjg0ZjkzMjUtNjUyNS00Yjk5LTgwNzktOTEyOGZjZWNlNGViIiwidGlkIjoiNDdiZTBhYmYtYzZhMS00ZjA0LWE2NjUtZGNlYjA4MWM0ZmYxIiwidXRpIjoiSkZjUE9SSHRGVTJMMWludEpkY2RBQSIsInZlciI6IjEuMCIsInhtc190Y2R0IjoxMzQ0Njc5MzA0fQ.fXEs7eClm5SYXychcKXbTfcc5gtvyyMa5fDWuGu2vqQ4Zc6V0jJSHSeksRiOzYE8SOJXRTmI9vJtbs2XIMFr0CRHeTgoCDReV8JWJ8yhOKiDnc-_2AHtSoBnqt6ibF0eX4AzkyioJd24-uYTSkheC_zDpd6GS3T5T077BU_1M7kpngXDfEICi38VkddcpdBUG8FgHUSPq0S9fCosIB4_JPwspq3QC6jJyoRrj1Yj2oR8FwBA1dpgWq_e0QoGnWXgT6EhBKedjY0hwHGY-F73ndvRlAKKW63JYucdOtRyC2zFDc4DPwhN1nyPlh86_Y0Zru8UTb0QgWRFKbGZwQcEOg

我尝试更改权限,并添加和删除了权限。

{
"error": {
    "code": "Authorization_RequestDenied",
    "message": "Insufficient privileges to complete the operation.",
    "innerError": {
        "request-id": "aa38f822-7325-44ad-9127-3cb4779578bf",
        "date": "2019-06-11T11:42:16"
    }
}

}

已更新::包括权限屏幕截图配置

permission and consent

JWT调试器的令牌输出:

{
  "aud": "https://graph.microsoft.com",
  "iss": "https://sts.windows.net/f77804fb-8607-4e96-9fae-231360cc82b7/",
  "iat": 1560273380,
  "nbf": 1560273380,
  "exp": 1560277280,
  "aio": "42ZgYKjulnV3u/vJZNN0gz3ld2ZpAwA=",
  "app_displayname": "clmapp",
  "appid": "82ad79f2-27c7-4304-92f6-e3ffdb637e72",
  "appidacr": "1",
  "idp": "https://sts.windows.net/f77804fb-8607-4e96-9fae-231360cc82b7/",
  "tid": "f77804fb-8607-4e96-9fae-231360cc82b7",
  "uti": "BpTbRLEb5ECSO3qjslIgAA",
  "ver": "1.0",
  "xms_tcdt": 1376441181
}

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方式:

权限:

确保您遵循permission

enter image description here

Azure门户上的授予权限:

步骤:1

Application Permission菜单上选择API permissions

enter image description here

步骤:2

User.ReadWrite.All部分下选择Application Permission,但是User.Read.All也没问题。

enter image description here

令牌请求格式:

URL: https://login.microsoftonline.com/YourTenant.onmicrosoft.com/oauth2/token

  

对于V2.0网址: https://login.microsoftonline.com/YourTenant.onmicrosoft.com/oauth2/v2.0/token

HTTP Verb: POST

grant_type:client_credentials
client_id:b603c7be-a866-4-e6921e61f925
client_secret:Vxf1SluKbguf3wE5oGl/2XDSeZ8wL/Yp8ns4sc=
resource:https://graph.microsoft.com
  

对于V2.0,范围将是:scope:https://graph.microsoft.com/.default

请参见以下屏幕截图:

enter image description here

解码令牌并确认权限:

您可以使用https://jwt.io/对令牌进行解码,以确保您具有所需的权限:请参见以下屏幕截图:

enter image description here

请求用户列表:

在此端点Token上使用您的https://graph.microsoft.com/v1.0/users请求。请参见下面的屏幕截图。我已成功获取所有用户列表。

enter image description here

  

注意:如果您仍有任何疑问,请随时与我们分享。谢谢,祝您编程愉快!

答案 1 :(得分:0)

使用OAuth 2.0客户端凭据授予流程时,您的范围值应将内置.default范围用于您要访问的资源。参考文章中提到了这一点:Microsoft identity platform and the OAuth 2.0 client credentials flow

在您的情况下,您尝试访问的资源是Microsoft Graph,因此令牌请求中的scope值应为https://graph.microsoft.com/.default

https://login.microsoftonline.com/{tenant-id-or-domain}/oauth2/v2.0/token
    &grant_type=client_credentials
    &client_id={client-id}
    &client_secret={client-secret}
    &scope=https%3a%2f%2fgraph.microsoft.com%2f.default

请注意,要成功执行此操作,必须配置应用程序所需的权限(Azure门户> Azure Active Directory>应用程序注册> API权限)并授予

Choose and grant required application permissions