【发布时间】:2021-04-12 17:50:11
【问题描述】:
根据doc,如果您想刷新访问令牌,可以通过提供 client_id、client_secret、grant_type、refresh_token 对“https://oauth2.googleapis.com/token”进行 HTTP 调用。
代码:
user = User.query.filter_by(email='daniyaldehleh@gmail.com').first()
with open('client_secret.json') as d:
d = json.load(d)
thecredentials = {
'client_id': d['web']['client_id'],
'client_secret':d['web']['client_secret'],
'refresh_token':user.refresh,
'grant_type': user.refresh,
'redirect_uri': "http://localhost:5000/",
'access_type':'offline'
}
req = requests.post(url='https://oauth2.googleapis.com/token', data = thecredentials)
print(req.text)
return str(req)
尽管根据一些研究添加了“redirect_uri”和“access_type”,但我不断得到:
{
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: 1//04JBbHWUWxwS3CgYIARAAGAQSNwF-L9IrZyyrHpxIoerJ4XZkAuGosXeeRHiYvdEQG7uF5EO5jWSC_-9mrRMAhmM30JHZvgyIhbM"
}
【问题讨论】:
-
错误描述清楚。您没有将
refresh_token设置为grant_type,而是将刷新令牌。 -
不是一回事吗?老实说,我被具体地弄糊涂了。应该是这样的:
refresh_token = user.refresh thecredentials = { 'client_id': d['web']['client_id'], 'client_secret':d['web']['client_secret'], 'refresh_token':refresh_token, 'grant_type': refresh_token} -
如果你能通过示例代码或其他东西澄清它会很棒。
-
这种情况下
grant_type的值必须是refresh_token。所以请把'grant_type': user.refresh,修改成'grant_type': 'refresh_token',再测试一下。 -
哦,好的。非常感谢!!
标签: python http google-api google-calendar-api google-api-python-client