2015-07-01 164 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

它已经在有效载荷中。 –

相关问题