2017-04-04 23 views
0

我試圖用django發送一個請求,以使用OAuth2從我的api獲取access_token。我執行此代碼:Django OAuth2 unsupported_grant_type

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) 

當我把這一請求,我得到這個錯誤:

{ '錯誤': 'unsupported_grant_type'}

感謝您的幫助!

回答

2

如果有人有興趣正確的請求是:

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) 
+0

感謝這個答案。我正在使用application/json作爲內容類型。切換到x-www-form-urlencoded是我需要更改以使其適用於client_credentials授權類型。 –

相關問題