微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为 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 举报,一经查实,本站将立刻删除。