谷歌OAuth API令牌请求出现404错误

3

我正在尝试从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编码,我都会收到相同的错误(我认为requests库会自动进行编码)。
以下是我发送的数据的详细信息(当然已经过审查)。
'client_id':'2-------------------------------------------0.apps.googleusercontent.com',
'client_secret': '5----------------------p',
'code': '4/A-------------------------- ... ------------------------------------fGE#',
'grant_type': 'authorization_code',
'redirect_uri': 'https://localhost'

我知道这个问题与我的非常相似,但提供的所有解决方案要么不起作用(url编码),要么不适用于我(其他所有内容)。
我正在使用官方文档作为参考。
这可能非常明显,就像我在这里问的大多数问题一样。
编辑 - 我尝试过了。
data = "code="+auth_code+"&client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&grant_type=authorization_code"

......无论是否进行url编码,都返回了400。

4个回答

9
对我来说问题是我发送了 GET 请求。你必须发送 POST 请求。

谢谢!我想知道为什么他们不返回类似于“405方法不允许”的内容,这样会更有帮助... - konkit
谢谢兄弟,你救了我的一天。 - Ahmed Abdalla

2

问题解决了。

我没有将我的重定向URI添加到授权列表中,因为如果你将应用程序类型设置为“其他”,该选项不会出现。我将其设置为“Web应用程序”(即使它不是),并添加了我的重定向URI,这样问题就解决了。


1

1
好的,忘了那个。"r = requests.post("https://googleapis.com/oauth2/v4/token",data=data,headers=headers)" 也是404错误。 - theParanoidAndroid

0
在我的极端情况下,我正在请求令牌传递。
            val tokenRequest = Request.Builder()
            .method(
                "POST",
                "".toRequestBody("application/x-www-form-urlencoded".toMediaType())
            )

上面的代码片段在我将“POST”更改为“post”之前无法正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接