Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

python 3.x - Running the same code for 2 files but the one which runs on weekly shows token expired error.( Token has been expired or revoked.)

I am trying to upload a file to google drive using pydrive. i have two scripts to run,one on a daily basis and the other one weekly. the point is i am using the same client.json and credential file (since i am uploading it to the same drive from same account) to run these scripts and the daily scripts runs without any issue but the one i run weekly shows this error. the script which run weekly has the same code in it and also it ran for one week after successfully authenticating it manually and for the second week it started showing the error token expired or revoked. my code in both files are like this

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)

i have a specific path for the drive_credentials file to get the credential file for all the scripts. The error it shows is like this:

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.

Like others, I'm trying to get the Google refresh token to work in order to run scheduled tasks Your answers are much appreciated Thanks in advance

question from:https://stackoverflow.com/questions/65879171/running-the-same-code-for-2-files-but-the-one-which-runs-on-weekly-shows-token-e

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...