2015-07-01 163 views
1

我在我的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" 
} 

我知道這裏有類似的問題上如此,但我還是沒能找出錯誤。

+0

嗨Archit我正在做同樣的事情,但使用PHP和我想知道什麼是這個變量「代碼」,你從哪裏得到它? –

+1

當用戶授權您的谷歌應用程序時,'代碼'作爲參數傳遞。您可以使用它來交換用戶的「訪問令牌」。 –

+1

非常感謝Archit –

回答

2

這是一個愚蠢的錯誤。我給了錯誤redirect_uri。無論如何,來自谷歌API的錯誤消息並沒有太大的幫助。

0

附加有效載荷

grant_type=authorization_code 
+1

它已經在有效載荷中。 –

相關問題