【发布时间】:2015-07-02 01:09:23
【问题描述】:
我正在我的 django 应用中设置 Google Oauth 2。我能够获取代码,但是当我尝试将其交换为访问令牌时,我收到 Bad Request 错误。这是我的代码:
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)
可能出了什么问题?
当我使用 POSTMAN(谷歌浏览器应用程序)尝试相同的操作时,我得到了
{
"error": "invalid_request",
"error_description": "Required parameter is missing: grant_type"
}
我知道这里有类似的问题,但我仍然无法找出错误。
【问题讨论】:
-
嗨 Archit 我正在做同样的事情,但使用的是 php,我想知道这个变量“代码”是什么,你从哪里得到它?
-
code在用户授权您的谷歌应用程序时作为参数传递。你可以用它来换取用户的access token。
标签: python django google-analytics oauth-2.0 google-oauth