Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 400 Google Oauth2中对访问令牌的错误请求_Python_Django_Google Analytics_Oauth 2.0_Google Oauth - Fatal编程技术网

Python 400 Google Oauth2中对访问令牌的错误请求

Python 400 Google Oauth2中对访问令牌的错误请求,python,django,google-analytics,oauth-2.0,google-oauth,Python,Django,Google Analytics,Oauth 2.0,Google Oauth,我正在django应用程序中设置Google Oauth 2。我能够获取代码,但当我尝试将其交换为访问令牌时,我得到了一个错误请求。这是我的代码: code = request.GET['code'] state = request.GET['state'] access_token_url = "https://www.googleapis.com/oauth2/v3/token" payload = { 'grant_type' : "authorization_code",

我正在django应用程序中设置Google Oauth 2。我能够获取代码,但当我尝试将其交换为访问令牌时,我得到了一个
错误请求。这是我的代码:

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(谷歌chrome应用程序)尝试同样的方法时,我得到了

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

添加到有效负载

grant_type=authorization_code

这是一个愚蠢的错误。我给出了错误的
重定向uri
。无论如何,来自google API的错误消息并没有多大帮助。

嗨,Archit,我正在做同样的事情,但使用php,我想知道这个变量“code”是什么,从哪里得到的?
code
在用户授权您使用google应用程序时作为参数传递。您可以使用它来交换用户的
访问令牌。
grant_type=authorization_code