我创建了刷新令牌来使用YouTube验证我的Python,并将其保存在我的系统上以备将来使用。但一周后,我发现即使是刷新令牌也过期了,我必须再次使用OAuth同意屏幕对应用程序进行身份验证。是否有方法绕过此刷新令牌过期限制,以便无需任何进一步身份验证即可调用API?
if os.path.exists(self.token):
print('Loading Credentials From File...')
with open(self.token, 'rb') as token:
self.credentials = pickle.load(token)
if not self.credentials or not self.credentials.valid:
if self.credentials and self.credentials.expired and self.credentials.refresh_token:
print('Refreshing Access Token...')
self.credentials.refresh(Request())
else:
print('Fetching New Tokens...')
flow = InstalledAppFlow.from_client_secrets_file(self.secret_file, self.scopes)
self.credentials = flow.run_console()
print(self.credentials.to_json())
# Save the credentials for the next run
with open(self.token, 'wb') as f:
print('Saving Credentials for Future Use...')
pickle.dump(self.credentials, f)
发布于 2021-03-16 22:36:00
刷新令牌过期的原因:
如果刷新令牌在六个月内未使用,它将过期如果用户撤销您在其帐户中的访问权限,则刷新令牌将被授权给您的应用程序。您将获得一个刷新令牌,有时当您刷新访问令牌时,会向您返回一个新的刷新令牌。在最旧的刷新令牌过期之前,您最多可以有50个未完成的刷新令牌。
我会仔细检查您是否存储了最新的刷新令牌,并设置您的代码来处理过期的刷新令牌并再次请求用户访问。
https://stackoverflow.com/questions/66657104
复制相似问题