Google OAuth API令牌请求出现404错误

时间:2018-05-17 16:54:32

标签: python oauth google-api google-oauth

我正在尝试从Google OAuth api获取令牌。我已经成功地获得了临时身份验证代码。

但是,我的请求返回404错误,正常的Google"这是一个错误" 404页。这是我的Python代码:

data = {
    "code":auth_code,
    "client_id":client_id,
    "client_secret":client_secret,
    "redirect_uri":redirect_uri,
    "grant_type":"authorization_code"
    }
headers = {"Content-Type":"application/x-www-form-urlencoded"}
r = requests.post("https://googleapis.com/oauth/v4/token",data=data,headers=headers)

无论我是否对我的参数进行url-encode(我认为请求库仍然执行此操作),我都会得到相同的错误。

以下是我发送的更详细的数据(当然是审查)

'client_id':'2-------------------------------------------0.apps.googleusercontent.com',
'client_secret': '5----------------------p',
'code': '4/A-------------------------- ... ------------------------------------fGE#',
'grant_type': 'authorization_code',
'redirect_uri': 'https://localhost'

我理解问题here与我的问题非常相似,但所有解决方案都提供了不工作(网址编码)或不适用(其他所有内容)。

我正在使用this官方文档作为参考。

这可能非常明显,就像我在这里提出的大多数问题一样。

编辑 - 我试过了

data = "code="+auth_code+"&client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&grant_type=authorization_code"

...返回400.有或没有url-encoding。

3 个答案:

答案 0 :(得分:1)

您的代码段列出" https://googleapis.com/oauth/v4/token"。

令牌端点是" https://googleapis.com/oauth2/v4/token"。

答案 1 :(得分:1)

对我来说,问题是我正在发送GET。您必须发送POST。

答案 2 :(得分:0)

想出来。

我没有将重定向uri添加到授权列表中,因为如果您将应用类型设置为“其他”,则不会显示该选项。我将它设置为“Web应用程序”(即使它不是)并添加了我的重定向uri并修复了它。

相关问题