【问题标题】:400 Bad Request in Google Oauth2 for access tokenGoogle Oauth2 中的 400 错误请求访问令牌
【发布时间】: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


【解决方案1】:

这是一个愚蠢的错误。我给错了redirect_uri。无论如何,来自 google API 的错误消息并没有太大帮助。

【讨论】:

    【解决方案2】:

    添加到有效负载

    grant_type=authorization_code
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 2012-11-06
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多