Microsoft graph API: Unable to fetch users with the generated access token

旧街凉风 提交于 2019-12-02 01:53:15

You could try following way:

Permission:

Make sure you have following permission:

Grant permission On Azure Portal:

Step:1

Select Application Permission On API permissions menu

Step:2

Select User.ReadWrite.All under Application Permission part But User.Read.All also alright.

Token Request Format:

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

For V2.0 URL: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

For V2.0 scope Will be : scope:https://graph.microsoft.com/.default

See the screen shot below:

Decode Token & Confirm permission:

You can use https://jwt.io/ to decode your token to make sure you have required permission: See the below screen shot:

Request For User List:

With your Token Request on this endpoint https://graph.microsoft.com/v1.0/users. See the screen shot below. I have successfully get all the user list.

Note: If you still have any concern feel free to share. Thanks and happy coding!

When using the OAuth 2.0 Client Credentials Grant flow, your scope value should use the built-in .default scope for the resource your are trying to access. This is mentioned in the reference article: Microsoft identity platform and the OAuth 2.0 client credentials flow

In your case, the resource you are trying to access is Microsoft Graph, so your scope value in the token request should be 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

Note that for this to succeed, the app's required permissions must be configured (Azure portal > Azure Active Directory > App registrations > API permissions) and granted:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!