您如何使用Authlib和GSpread刷新身份验证令牌?

问题描述 投票:1回答:1

我正在python应用程序中使用gspread来访问一些后端Google表格,这些后端Google表格用作该应用程序的数据库。要求用户使用应用内浏览器登录到应用,然后使用url中通过此方法返回的令牌创建authlib OAuth2会话。最初的登录和访问工作正常,但是当令牌在一个小时后超时时,我将访问工作表,并且应用程序中断。

我无法确定如何使用authlib刷新令牌,我们选择该库的原因是因为它与gspread集成在一起,并且应该为您自动刷新auth令牌。我用于登录的代码如下,但是一个小时后,由于身份验证问题,gspread函数open_by_key()失败了。

我已经尝试过重新创建gspread客户端,但这只是使用相同的令牌。我觉得应该有一种刷新令牌的方法,将令牌的寿命再延长一个小时,但我找不到如何使用authlib进行此操作。

creds = OAuth2Session(clientId, clientSecret, scope=scope, redirect_uri=redirectUrl)
authUrl, state = creds.create_authorization_url('https://accounts.google.com/o/oauth2/v2/auth', response_type='token')

在浏览器中加载authURL并让用户登录。浏览器将身份验证响应作为urlString返回>

creds.fetch_access_token(authorization_response=urlString)

gc = gspread.Client(None, creds)
ss = gc.open_by_key(sheetKey)
todaysSheetName = datetime.datetime.now().strftime('%d-%m-%Y')
wks = ss.worksheet(todaysSheetName)

我正在python应用程序中使用gspread来访问一些后端Google表格,这些后端Google表格用作该应用程序的数据库。要求用户使用应用内浏览器和authlib登录应用。...

python authentication gspread authlib
1个回答
0
投票
authUrl, state = creds.create_authorization_url('https://accounts.google.com/o/oauth2/v2/auth', response_type='token')
© www.soinside.com 2019 - 2024. All rights reserved.