Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 655
antoinv10 Avatar asked Dec 19 '22 08:12

antoinv10


2 Answers

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)
like image 194
antoinv10 Avatar answered Feb 27 '23 13:02

antoinv10


If you don't want to encode data in url, you can put this in your settings.

OAUTH2_PROVIDER = {                                                                                                                                                                      
    'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore',                                                                                                          
}
like image 41
Mehdi Pourfar Avatar answered Feb 27 '23 15:02

Mehdi Pourfar