Search code examples
djangooauth-2.0

Django OAuth2 unsupported_grant_type


I'm trying to send a request with django to get an access_token from my api using OAuth2. I'm executing this code :

data = {'username': 'admin', 'password': '123123', 'grant_type': 
'password','client_id': 'xxx','client_secret': 'xxx'}
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=data, headers=headers)

When I send this request I get this error :

{'error': 'unsupported_grant_type'}

Thanks for your help !


Solution

  • If anyone is interested the correct request was :

        payload = "grant_type=password&client_secret=xxx&client_id=xxx&username=username&password=password"
        headers = {
            'content-type': "application/x-www-form-urlencoded",
            'cache-control': "no-cache",
        }
    
        response = requests.request("POST", url, data=payload, headers=headers)