使用带有烧瓶的Authlib获取和存储刷新令牌



我正在使用authlib https://github.com/lepture/authlib 获取用户对其数据的身份验证,因此每日离线调度程序可以代表用户下载一些数据。

我首先注册客户端:

google = oauth.register(
'google',
client_id = '***',
client_secret = '***',
access_token_url = "https://www.googleapis.com/oauth2/v4/token", 
access_token_params = None,
authorize_url = "https://accounts.google.com/o/oauth2/v2/auth",
authorize_params = None,
api_base_url = 'https://googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'https://www.googleapis.com/auth/doubleclickbidmanager'}
)

在稍后阶段,我使用以下方法检索令牌:

token = oauth.google.authorize_access_token()

当我打印令牌时,我看到 Google 没有返回我需要存储在数据库中以供离线使用的刷新令牌:

{'access_token': '***', 'expires_in': 3599, 'scope': 'https://www.googleapis.com/auth/doubleclickbidmanager', 'token_type': 'Bearer', 'expires_at': 1591750317}

我是否可以更改向 access_type = 离线注册客户端的方式,以便让 Google 知道我也需要刷新令牌?如何获取和存储刷新令牌?

以下是将access_type添加到授权端点的解决方案:

google = oauth.register(
'google',
# ...
authorize_params={'access_type': 'offline'},
)

使用此authorize_params将额外的参数传递给authorize_url。

最新更新