How to use Microsoft.Graph with client authorizati

2019-07-03 23:02发布

问题:

We have

  • created Azure Active Directory
  • created a new Application
  • registered the new Application in Azure AD
  • given ALL the permissions to the API
  • granted admin permissions

Now we login with this app credentials

We get authorization:

GET /1111111-aaaa-4fd3-bf32-9bfc460f67b1/oauth2/v2.0/authorize HTTP/1.1
Host: login.microsoftonline.com
cache-control: no-cache
Postman-Token: 111111-aaaa-4696-9828-9110670706c5

We get the token:

POST /11111111-aaaa-4fd3-bf32-9bfc460f67b1/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: 11111111-aaaa-47e4-b2fb-6b94cd02312a
client_id=11111111-aaaa-482e-ab17-86e4c2c6240c
scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
client_secret=aaaaaaaaaaaDJk6jBJ%2FuBRJM6AFxyFIDMKn867hmvU%3D
grant_type=client_credentials

The token analyzed with jwt.io:

{
  "aud": "https://graph.microsoft.com",
  "iss": "https://sts.windows.net/1111111-ccc6-4fd3-bf32-9bfc460f67b1/",
  "iat": 1541603660,
  "nbf": 1541603660,
  "exp": 1541607560,
  "aio": "42RgYFB+/Gyh8uF3yl+uKU7km//kFwA=",
  "app_displayname": "ToDoListService",
  "appid": "1111111-4e99-482e-ab17-86e4c2c6240c",
  "appidacr": "1",
  "idp": "https://sts.windows.net/1111111-ccc6-4fd3-bf32-9bfc460f67b1/",
  "oid": "1111111-98fb-4a47-af7f-d3800dde2f7b",
  "roles": [
    "Chat.UpdatePolicyViolation.All",
    "Calls.JoinGroupCall.All",
    "EduRoster.Read.All",
    "OnlineMeetings.Read.All",
    "Mail.ReadWrite",
    "OnlineMeetings.ReadWrite.All",
    "Device.ReadWrite.All",
    "User.ReadWrite.All",
    "Domain.ReadWrite.All",
    "Application.ReadWrite.OwnedBy",
    "SecurityEvents.Read.All",
    "Calendars.Read",
    "EduAssignments.ReadWrite.All",
    "People.Read.All",
    "Application.ReadWrite.All",
    "Calls.InitiateGroupCall.All",
    "Group.Read.All",
    "Directory.ReadWrite.All",
    "EduAssignments.ReadWriteBasic.All",
    "MailboxSettings.Read",
    "EduAdministration.Read.All",
    "Calls.JoinGroupCallAsGuest.All",
    "Sites.Read.All",
    "Sites.ReadWrite.All",
    "Contacts.ReadWrite",
    "Group.ReadWrite.All",
    "Sites.Manage.All",
    "SecurityEvents.ReadWrite.All",
    "Notes.Read.All",
    "User.Invite.All",
    "EduRoster.ReadWrite.All",
    "Files.ReadWrite.All",
    "Directory.Read.All",
    "User.Read.All",
    "EduAssignments.ReadBasic.All",
    "EduRoster.ReadBasic.All",
    "Files.Read.All",
    "Mail.Read",
    "Chat.Read.All",
    "ChannelMessage.Read.All",
    "EduAssignments.Read.All",
    "Calendars.ReadWrite",
    "identityriskyuser.read.all",
    "EduAdministration.ReadWrite.All",
    "Mail.Send",
    "ChannelMessage.UpdatePolicyViolation.All",
    "MailboxSettings.ReadWrite",
    "Contacts.Read",
    "IdentityRiskEvent.Read.All",
    "AuditLog.Read.All",
    "Member.Read.Hidden",
    "Calls.AccessMedia.All",
    "Sites.FullControl.All",
    "Reports.Read.All",
    "Calls.Initiate.All",
    "Notes.ReadWrite.All"
  ],
  "sub": "1111111-98fb-4a47-af7f-d3800dde2f7b",
  "tid": "1111111-ccc6-4fd3-bf32-9bfc460f67b1",
  "uti": "hxPwbjRRm0y6SI8hxuckAA",
  "ver": "1.0",
  "xms_tcdt": 1541414851
}

You can see that we get all the necesary permissions.

And this petition works:

https://graph.microsoft.com/v1.0/Users/

or

https://graph.microsoft.com/beta/Users/

but not this one:

https://graph.microsoft.com/beta/Users/<user-id>/Calendars

nor this one:

https://graph.microsoft.com/beta/users/<user-id>/sendMail

they both return 401:

{
  "error": {
    "code": "UnknownError",
    "message": "",
    "innerError": {
      "request-id": "019ddc83-6528-4f19-95cc-8b99ea53a046",
      "date": "2018-11-07T16:03:55"
    }
  }
}

I think that we had got all the necessary permission, configuration... etc... so why isn't it working? :'(

[UPDATE]

detailed call to get calendars

GET /v1.0/Users/<user-id>/Calendars HTTP/1.1
cache-control: no-cache
Postman-Token: xxxxxxxxxxxxxxxxxxxxxxx
Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
User-Agent: PostmanRuntime/7.3.0
Accept: */*
Host: graph.microsoft.com
accept-encoding: gzip, deflate
Connection: close

Response

HTTP/1.1 401 Unauthorized
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/json
request-id: 11111111-aaaa-aaaa-a115-e09bb6f09917
client-request-id: 11111111-aaaa-aaaa-a115-e09bb6f09917
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Europe","Slice":"SliceC","Ring":"3","ScaleUnit":"001","Host":"AGSFE_IN_34","ADSiteName":"NEU"}}
Duration: 377.0084
Strict-Transport-Security: max-age=31536000
Date: Thu, 08 Nov 2018 09:16:03 GMT
Connection: close

c6
{
  "error": {
    "code": "UnknownError",
    "message": "",
    "innerError": {
      "request-id": "2bb9a7c1-f8a3-4c1f-a115-e09bb6f09917",
      "date": "2018-11-08T09:16:04"
    }
  }
}
0

回答1:

let me start by first of all noticing that you have posted a client secret on a public forum :) And one that has luxurious permission on your tenant. I hope you have changed the client secret by now.

But to get back to your question - I will have to ask a bit more in detail to get a better understanding of what your actual results were:

  1. According to your JWT printout you are getting a V1 token but using the client credential flow (the field "ver" is set to 1.0). The Microsoft Documentation here Get access without a user explicitly states that you should use a V2 Token to access Microsoft Graph in this scenario. So the first question is: Did you create your Azure App using the V2 Portal: https://apps.dev.microsoft.com/ ? If not then this may be the issue.
  2. A more common issue when manually testing - Have you made sure that the token had not expired from the time you got it from the /token endpoint up to the time you tested your API call?
  3. If none of the above - please post your request to the https://graph.microsoft.com/beta/Users//Calendars endpoint in detail, the devil may be in the details