如何使用 python 中的刷新 token 刷新 google 中的访问 token ?我有刷新 token 、客户端 ID 和客户端 key
我尝试使用以下代码来生成访问 token (过期后)
params = {
"grant_type": "refresh_token",
"client_id": client_id,
"client_secret": client_secret,
"refresh_token": refresh_token
}
authorization_url = "https://www.googleapis.com/oauth2/v4/token"
r = requests.post(authorization_url, data=params)
print(r.text)
if r.ok:
return r.json()['access_token']
else:
return None
我收到这样的响应错误:
{
"error": "invalid_grant",
"error_description": "Bad Request"
}
但我需要生成访问 token 。
最佳答案
这是它不起作用的三个主要原因。我不是 Python 开发人员,因此无法测试您的代码,抱歉。
以字符串形式发送参数
检查您的参数,确保它们作为用 & 分隔的单个字符串发送。
Post https://accounts.google.com/o/oauth2/token
client_id={ClientId}&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token
内容类型 header
此外,我认为您需要将内容类型 header 设置为 application/x-www-form-urlencoded
之类的内容。
更改端点
如果这不起作用
另请尝试使用 https://accounts.google.com/o/oauth2/token
而不是 https://www.googleapis.com/oauth2/v4/token
code> 我记得有时其中一个或另一个会出现一些问题
关于python - 使用刷新 token 刷新谷歌访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54530510/