【发布时间】:2015-01-24 14:56:40
【问题描述】:
我正在使用FLask Framework 构建一个网站+ 后端,其中我使用Flask-OAuthlib 向谷歌进行身份验证。身份验证后,后端需要定期扫描用户他的Gmail。所以目前用户可以验证我的应用程序,我存储access_token 和refresh_token。 access_token 在一小时后过期,所以在一小时内我可以像这样获得用户信息:
google = oauthManager.remote_app(
'google',
consumer_key='xxxxxxxxx.apps.googleusercontent.com',
consumer_secret='xxxxxxxxx',
request_token_params={
'scope': ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/gmail.readonly'],
'access_type': 'offline'
},
base_url='https://www.googleapis.com/oauth2/v1/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://accounts.google.com/o/oauth2/token',
authorize_url='https://accounts.google.com/o/oauth2/auth'
)
token = (the_stored_access_token, '')
userinfoObj = google.get('userinfo', token=token).data
userinfoObj['id'] # Prints out my google id
一小时结束后,我需要使用 refresh_token(我已存储在我的数据库中)来请求新的access_token。我尝试用the_stored_refresh_token 替换the_stored_access_token,但这只是给了我一个Invalid Credentials-error。
在this github issue 我读到以下内容:
无论您如何获得访问令牌/刷新令牌(无论是通过授权代码授予还是资源所有者密码凭据),您都可以通过将刷新令牌作为 refresh_token 并将 grant_type 设置为“refresh_token”来交换它们。
从这里我明白我必须像这样创建一个远程应用程序:
google = oauthManager.remote_app(
'google',
# also the consumer_key, secret, request_token_params, etc..
grant_type='refresh_token',
refresh_token=u'1/xK_ZIeFn9quwvk4t5VRtE2oYe5yxkRDbP9BQ99NcJT0'
)
但这会导致TypeError: __init__() got an unexpected keyword argument 'refresh_token'。所以从这里我有点迷路了。
有人知道我如何使用refresh_token 来获得新的access_token 吗?欢迎所有提示!
【问题讨论】:
标签: python oauth flask google-api flask-oauthlib