Share via

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

Answer accepted by question author
  1. Pravallika KV 13,725 Reputation points Microsoft External Staff Moderator
    2026-01-26T01:14:52.7366667+00:00

    Hi @John Selby ,

    Thanks for reaching out to Microsoft Q&A.

    The error message you're seeing, 'request body must contain grant_type', typically indicates that there's an issue with the way you're formatting your data for the POST request.

    You can use below code which worked for me:

    import requests
    import json
    
    def get_new_token():
    
        client_id = 
        client_secret = 
        tenant_id = 
    
        scope = "https://graph.microsoft.com/.default"
    
        token_url = f"https://login.microsoftonline.com/{
    
        payload = {
            "grant_type": "client_credentials",
            "client_id": 
            "client_secret": 
            "scope": scope
        }
    
        headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    
        response = requests.post(token_url, data=payload, headers=headers)
    
        response.raise_for_status()
    
        token_response = response.json()
    
        print("New Token:", token_response.get("access_token"))
        print("Response Type:", type(response))
        print("HTTP Status Code:", response.status_code)
        print("Raw Response Content:", response.content)
    
        return token_response.get("access_token")
    
    token = get_new_token()
    

    Output:

    New Token: <Token_generated>
    Response Type: <class 'requests.models.Response'>
    HTTP Status Code: 200
    Raw Response Content: b'{"token_type":"Bearer","expires_in":3599,"ext_expires_in":3599,"access_token":"eyJ0eXXXXXXLCecZw"}'
    

    Hope this helps!


    If the resolution was helpful, kindly take a moment to click on 210246-screenshot-2021-12-10-121802.pngand click on Yes for was this answer helpful. And, if you have any further query do let us know.


0 additional answers

Sort by: Most helpful

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.