0

I'm writing code to access the MS365 API and the Python code example uses urllib. I want to instead use requests but I'm not sure how urllib translates into requests as my attempts of doing so have failed.

The code example can be found here:

https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/run-advanced-query-sample-python?view=o365-worldwide#get-token

import json import urllib.request import urllib.parse tenantId = '00000000-0000-0000-0000-000000000000' # Paste your own tenant ID here appId = '11111111-1111-1111-1111-111111111111' # Paste your own app ID here appSecret = '22222222-2222-2222-2222-222222222222' # Paste your own app secret here url = "https://login.microsoftonline.com/%s/oauth2/token" % (tenantId) resourceAppIdUri = 'https://api.securitycenter.microsoft.com' body = { 'resource' : resourceAppIdUri, 'client_id' : appId, 'client_secret' : appSecret, 'grant_type' : 'client_credentials' } data = urllib.parse.urlencode(body).encode("utf-8") req = urllib.request.Request(url, data) response = urllib.request.urlopen(req) jsonResponse = json.loads(response.read()) aadToken = jsonResponse["access_token"] 

2 Answers 2

1

IIUC, this should work the same:

import requests tenantId = '00000000-0000-0000-0000-000000000000' # Paste your own tenant ID here appId = '11111111-1111-1111-1111-111111111111' # Paste your own app ID here appSecret = '22222222-2222-2222-2222-222222222222' # Paste your own app secret here url = "https://login.microsoftonline.com/%s/oauth2/token" % (tenantId) resourceAppIdUri = 'https://api.securitycenter.microsoft.com' params = { 'resource' : resourceAppIdUri, 'client_id' : appId, 'client_secret' : appSecret, 'grant_type' : 'client_credentials' } response = requests.get(url, params) jsonResponse = response.json() aadToken = jsonResponse["access_token"] 
Sign up to request clarification or add additional context in comments.

2 Comments

The content of this response is an HTML page saying the endpoint only accepts POST. Changing it to requests.post(url=url, data=params) has made it work.
Ah, interesting, didn't realize it was a post request. Glad you got it to work!
0

Modifying @BeRT2me's answer has made this work.

import requests tenantId = '00000000-0000-0000-0000-000000000000' # Paste your own tenant ID here appId = '11111111-1111-1111-1111-111111111111' # Paste your own app ID here appSecret = '22222222-2222-2222-2222-222222222222' # Paste your own app secret here url = "https://login.microsoftonline.com/%s/oauth2/token" % (tenantId) resourceAppIdUri = 'https://api.securitycenter.microsoft.com' data = { 'resource' : resourceAppIdUri, 'client_id' : appId, 'client_secret' : appSecret, 'grant_type' : 'client_credentials' } response = requests.post(url=url, data=data) jsonResponse = response.json() aadToken = jsonResponse["access_token"] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.