Python Google API无效\u授权/错误\u请求

Python Google API无效\u授权/错误\u请求,python,python-3.x,python-requests,google-oauth,Python,Python 3.x,Python Requests,Google Oauth,使用Python3.6,请求==2.22.0 正在尝试使用GoogleAPI,尤其是 我可以使用以下url生成身份验证代码: url = ( 'https://accounts.google.com/o/oauth2/v2/auth?' 'scope=email%20profile&' 'response_type=code&' 'redirect_uri={redirect_uri}&' 'client_id={client_id

使用Python3.6,请求==2.22.0

正在尝试使用GoogleAPI,尤其是

我可以使用以下url生成身份验证代码:

url = (
    'https://accounts.google.com/o/oauth2/v2/auth?'
    'scope=email%20profile&'
    'response_type=code&'
    'redirect_uri={redirect_uri}&'
    'client_id={client_id}&'
    'access_type=offline'.format(
        redirect_uri=redirect_uri,
        client_id=client_id,
    )
)
我(目前)使用的重定向uri只是
https://google.com
,并在我生成的开发者应用程序中注册,在
授权重定向URI
部分
授权域
部分
OAuth同意设置
页面/选项卡下

一旦我将生成的url粘贴到浏览器中,我就会得到一个代码,可以提取并用于进行下一次调用,但当前调用失败

我确保将
“%2F”
替换为
“/”
(使用Firefox时)

接下来,我将生成数据,并调用以令牌替换代码:

data = {
    'client_id': client_id,
    'client_secret': client_secret,
    'grant_type': 'authorization_code',
    'code': code,
    'redirect_uri': redirect_uri,
}
url = 'https://oauth2.googleapis.com/token'

response = requests.post(
    url,
    data=data,
    headers={
        'Content-Type': 'application/x-www-form-urlencoded',
    },

print(response)
print(response.json())
输出:

<Response [400]>
{'error': 'invalid_grant', 'error_description': 'Bad Request'}

{'error':'invalid_grant','error_description':'Bad Request'}

Invalid grant通常意味着您发送的客户id/secret/code无效。您的e right@DaImTo-是代码-我不正确地清理了它