python - Google Oauth2 中的 400 错误请求访问 token

标签 python django google-analytics oauth-2.0 google-oauth

我正在我的 django 应用程序中设置 Google Oauth 2。我能够获取代码,但是当我尝试将其交换为访问 token 时,我收到 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"
}

我知道这里有类似的问题,但我仍然无法找出错误。

最佳答案

这是一个愚蠢的错误。我提供了错误的 redirect_uri。不管怎样,来自 google API 的错误消息没有多大帮助。

关于python - Google Oauth2 中的 400 错误请求访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158590/

相关文章:

android - 谷歌分析 : how to report user scope custom dimentions

angularjs - Google 分析和 SPA 分析的屏幕浏览次数始终为零

ruby - Google Analytics API - 分页

python - Django 模板中变量的使用 {% block %} 标签

python - 如何从 Django 管理上传多个文件?

Django:任何使用 blocktrans 都会导致 TemplateSyntaxError

python - Django - 新字体?

python - 在Python的类中将列表从一个函数传递给另一个函数

python - CMake 运行脚本

python - 一起使用 pytest fixtures 和 peewee transactions