question

DominikaGontarzPL-5295 avatar image
0 Votes"
DominikaGontarzPL-5295 asked RakeshJagatap-4451 commented

MS Graph token incomplete

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

azure-active-directory
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, if the posted answer resolves your question, please mark it as the answer by clicking the check mark. Doing so helps others find answers to their questions.

0 Votes 0 ·

1 Answer

RoderickBant74 avatar image
0 Votes"
RoderickBant74 answered

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)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.