如何使用refresh_token获取新的access_token(使用Flask-OAuthLib)?

kra*_*r65 6 python oauth google-api flask flask-oauthlib

我正在使用FLask Framework构建一个网站+后端,我在其中使用Flask-OAuthlib与google进行身份验证.身份验证后,后端需要定期扫描用户的Gmail.因此,目前用户可以验证我的应用程序,我存储access_tokenrefresh_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
Run Code Online (Sandbox Code Playgroud)

小时结束后,我需要使用refresh_token(我已经存储在我的数据库中)来请求新的access_token.我试着更换the_stored_access_tokenthe_stored_refresh_token,但是这只是给了我一个Invalid Credentials-错误.

这个github问题中,我阅读了以下内容:

无论您如何获得访问令牌/刷新令牌(无论是通过授权代码授予还是资源所有者密码凭据),您都可以通过将刷新令牌作为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'
    )
Run Code Online (Sandbox Code Playgroud)

但这导致了一个TypeError: __init__() got an unexpected keyword argument 'refresh_token'.所以从这里我有点失落.

有谁知道如何使用它refresh_token来获得新的access_token?欢迎所有提示!

pec*_*rin 0

查看OAuthRemoteApp的源代码。该构造函数不采用名为“refresh_token”的关键字参数。然而,它确实需要一个名为access_token_params“which is”的参数an optional dictionary of parameters to forward to the access token url

由于url相同,但授权类型不同。我想这样的调用应该有效:

google = oauthManager.remote_app(
    'google',
    #  also the consumer_key, secret, request_token_params, etc..
    grant_type='refresh_token',
    access_token_params = {
       refresh_token=u'1/xK_ZIeFn9quwvk4t5VRtE2oYe5yxkRDbP9BQ99NcJT0'
    }
)
Run Code Online (Sandbox Code Playgroud)