Share via

MS Graph token incomplete

Dominika 1 Reputation point
2021-08-25T21:09:49.347+00:00

Hello,

I am trying to get MS Graph token using API to use other API calls.

Here is my code to get token(python):

data = {'client_id': 'f4ea40d5-6ec2-44fa-8364-cexxxx','client_secret':'rsxxxxx', 'grant_type':'client_credentials', 'scope':'https://graph.microsoft.com/.default'}
headers = {'Content-Type':'application/x-www-form-urlencoded'}
url = 'https://login.microsoftonline.com/a0821be3-1cfa-48e4-b291-be2x5/oauth2/v2.0/token'
response = requests.post(url, headers=headers, data=data)

In the results of the code I am receiving token, but it is incomplete - is shorter than token from MS Graph console.

Could you tell me if my python code is correct to get token?

Regards,
Dominika

Microsoft Security | Microsoft Entra | Microsoft Entra ID

1 answer

Sort by: Most helpful
  1. Roderick Bant 2,056 Reputation points
    2021-09-05T14:55:53.42+00:00

    The code below works for me to get acces to Intune data via graph in Python

    import requests, json
    # Authenticate with Azure Management API
    az_auth_post_body = { 'grant_type': 'client_credentials',
                          'client_id': "ADD YOUR CLIENT ID HERE",
                          'client_secret': "ADD YOUR CLIENT SECRET HERE",
                          'resource': 'https://graph.microsoft.com',
                          'scope': 'https://graph.microsoft.com'
    }
    az_auth_url = f"https://login.microsoftonline.com/<< ADD YOUR TENANT ID HERE >>/oauth2/token"
    az_auth_result = requests.post(az_auth_url, data = az_auth_post_body )
    az_access_token = json.loads(az_auth_result.content)['access_token']
    az_auth_header = { 'Authorization': f"Bearer { az_access_token }"}
    managed_device_overview_url = "https://graph.microsoft.com/v1.0/deviceManagement/managedDeviceOverview"
    managed_device_overview_result = requests.get(
          managed_device_overview_url,
          headers = az_auth_header
        )
    print(managed_device_overview_result.content)
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.