Search code examples
phpazurecurlazure-active-directory

Authorization_RequestDenied error on https://graph.microsoft.com/v1.0/users


I am trying to fetch miscrosoft graph api user. For that I have generated token like this.

enter image description here

After getting token, I am using the token to fetch users using this api url

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

I have given all the needed permission to the API. but still getting following error

{
    "error": {
        "code": "Authorization_RequestDenied",
        "message": "Insufficient privileges to complete the operation.",
        "innerError": {
            "date": "2024-01-02T02:54:50",
            "request-id": "******************",
            "client-request-id": "******************"
        }
    }
}   

Admin consent also has been assigned to the application as shown in below screenshot.

enter image description here

UPDATE

enter image description here


Solution

  • Note that: To fetch the users, the Microsoft Entra application must have User.Read.All Application API permission granted as you are making use of Client credential flow.

    Make sure to grant User.Read.All Application API permission to the application:

    enter image description here

    I generated the access token by using below parameters:

    https://login.microsoftonline.com/TenantID/oauth2/token
    
    client_id:ClientID
    client_secret:ClientSecret
    resource:https://graph.microsoft.com
    grant_type:client_credentials
    

    enter image description here

    By using the above token, I am able to fetch the users successfully:

    GET https://graph.microsoft.com/v1.0/users
    

    enter image description here

    Reference:

    List users - Microsoft Graph v1.0 | Microsoft