python request.post for oauth2 token error

John Selby 20 Reputation points
2026-01-25T12:20:13.93+00:00

Below my code to get an oauth2 token,which always returns a 900114 error 'request body must contain grant_type' However if data contains only grant_type then it errors because scope is missing

I dont seem to be able to put both grant_type and scope in data

where am i going wrong?

#ver 24/01/26

import requests

import json

import sys

def get_new_token():

client_id = "xxxx"  

client_secret = "yyyy" 

tenant_id = "zzzz" 

scopes= "https://graph.microsoft.com/.default"

auth_server_url =  f"https://login.microsoftonline.com/{



data={'grant_type':'client_credentials','scope':scopes}

auth_header =  (client_id,client_secret)



response = requests.post(auth_server_url,json =data,auth=auth_header,headers={"Content-Type":"application/x-www-form-urlencoded"})



token_response=response.json()

print("New Token: ", token_response.get('access_token'))

print(type(response))

print(response.status_code)

print(response.content)

tokens = json.loads(response.text)
token = get_new_token()

Azure DevOps

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

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.