为 2 个文件运行相同的代码,但每周运行的文件显示令牌过期错误令牌已过期或撤销

问题描述

我正在尝试使用 pydrive 将文件上传到谷歌驱动器。 我有两个脚本要运行,一个每天运行,另一个每周运行。 关键是我使用相同的 client.json 和凭证文件(因为我从同一个帐户将其上传到同一个驱动器)来运行这些脚本,并且日常脚本运行没有任何问题,但我每周运行的脚本显示错误。 每周运行的脚本中包含相同的代码,并且在手动成功对其进行身份验证后运行了一周,第二周它开始显示错误令牌已过期或已撤销。 我在两个文件中的代码都是这样的

gauth = GoogleAuth()
gauth.LoadCredentialsFile("drive_credentials")
try:
    if gauth.credentials is None:
    # Authenticate if not present
        gauth.GetFlow()
        gauth.flow.params.update({'access_type': 'offline'})
        gauth.flow.params.update({'approval_prompt': 'force'})
        gauth.LocalWebserverAuth()
    elif gauth.access_token_expired:
    # Refresh  if expired
        gauth.Refresh()
    else:
    # Initialize the saved creds
        gauth.Authorize()
except Exception as e:
    logger.exception("Exception Occured",exc_info=True)
# Save the credentials to a file
gauth.SaveCredentialsFile("drive_credentials")
drive=GoogleDrive(gauth)

我有一个特定的 drive_credentials 文件路径来获取所有脚本的凭据文件。 它显示错误是这样的:

Traceback (most recent call last):
  File "/home/appuser/uber_scripts/uber_env/lib/python3.6/site-packages/pydrive/auth.py",line 475,in Refresh
    self.credentials.refresh(self.http)
  File "/home/appuser/uber_scripts/uber_env/lib/python3.6/site-packages/oauth2client/client.py",line 545,in refresh
    self._refresh(http)
  File "/home/appuser/uber_scripts/uber_env/lib/python3.6/site-packages/oauth2client/client.py",line 761,in _refresh
    self._do_refresh_request(http)
  File "/home/appuser/uber_scripts/uber_env/lib/python3.6/site-packages/oauth2client/client.py",line 819,in _do_refresh_request
    raise HttpAccesstokenRefreshError(error_msg,status=resp.status)
oauth2client.client.HttpAccesstokenRefreshError: invalid_grant: Token has been expired or revoked.
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
  File "upload_file.py",line 102,in <module>
    gauth.Refresh()
  File "/home/appuser/uber_scripts/uber_env/lib/python3.6/site-packages/pydrive/auth.py",line 477,in Refresh
    raise RefreshError('Access token refresh Failed: %s' % error)
pydrive.auth.RefreshError: Access token refresh Failed: invalid_grant: Token has been expired or revoked.

像其他人一样,我正在尝试让 Google 刷新令牌工作以运行计划任务 非常感谢您的回答 提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)