我在我的django應用程序中設置Google Oauth 2。我能夠獲得代碼,但是當我嘗試將其交換爲訪問令牌時,我得到一個Bad Request
錯誤。這是我的代碼:400谷歌Oauth2中的訪問令牌的錯誤請求
code = request.GET['code']
state = request.GET['state']
access_token_url = "https://www.googleapis.com/oauth2/v3/token"
payload = {
'grant_type' : "authorization_code",
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': code,
'redirect_uri': "http://127.0.0.1:8888/home",
}
payload = urllib.urlencode(payload)
r = urllib2.Request(access_token_url, payload, headers={"Content-type" : "application/x-www-form-urlencoded"})
response = urllib2.urlopen(r)
什麼可能是錯的?
當我嘗試使用相同的郵差(谷歌瀏覽器的應用程序),我得到
{
"error": "invalid_request",
"error_description": "Required parameter is missing: grant_type"
}
我知道這裏有類似的問題上如此,但我還是沒能找出錯誤。
嗨Archit我正在做同樣的事情,但使用PHP和我想知道什麼是這個變量「代碼」,你從哪裏得到它? –
當用戶授權您的谷歌應用程序時,'代碼'作爲參數傳遞。您可以使用它來交換用戶的「訪問令牌」。 –
非常感謝Archit –