iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
581 stars 69 forks source link

TokenRefreshError: invalid_grant: Bad Request #152

Closed drscotthawley closed 2 years ago

drscotthawley commented 2 years ago

Dear Ivan (et al), Thank you for creating such a helpful tool. I have some code that used to work in back October, but now that I'm re-running it on the same computer, in the same environment, with no changes to the code, I'm getting an error when I try to call FetchMetaData(). For example,

    print("calling createfile")
    gd_file = drive.CreateFile({'id': id})   # id is a valid file id
    print(f"Success: gd_file = {gd_file}, now calling FetchMetadata")   # this works ok
    gd_file.FetchMetadata(fields='modifiedDate')   # this generates the error

Output log looks like:

calling createfile
Success.  gd_file = GoogleDriveFile({'id': ' [id omitted]  '}), now calling FetchMetadata
Traceback (most recent call last):
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/pydrive2/auth.py", line 570, in Refresh
    self.credentials.refresh(self.http)
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/oauth2client/client.py", line 545, in refresh
    self._refresh(http)
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/oauth2client/client.py", line 761, in _refresh
    self._do_refresh_request(http)
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/oauth2client/client.py", line 819, in _do_refresh_request
    raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/student/autograding/./autograder.py", line 285, in <module>
    download_if_newer_gdrive(csv_url, ss_file)
  File "/home/student/autograding/./autograder.py", line 100, in download_if_newer_gdrive
    url_date = gdrive_file_date(url)
  File "/home/student/autograding/./autograder.py", line 77, in gdrive_file_date
    gd_file.FetchMetadata(fields='modifiedDate')
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/pydrive2/auth.py", line 60, in _decorated
    self.auth.LocalWebserverAuth()
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/pydrive2/auth.py", line 130, in _decorated
    self.Refresh()
  File "/home/student/envs/dlaie/lib/python3.9/site-packages/pydrive2/auth.py", line 572, in Refresh
    raise RefreshError("Access token refresh failed: %s" % error)
pydrive2.auth.RefreshError: Access token refresh failed: invalid_grant: Bad Request

The only change I can think of -- and I mention this because I noticed threads elsewhere about oauth2 failing if the system time is not correct -- is that I wrote the code & generated the credentials at home in Central time zone, and I'm currently travelling and running the code in Pacific time zone.
BUT: I just forced an system time update with via sudo service ntp stop; sudo ntpdate time.nist.gov ..and I still get the same error as above.

Do you have any suggestions for getting around this? Thanks!

PS- My settings.yaml file reads as:

client_config_backend: file
client_config:
    client_id: [omitted]
    client_secret: [omitted]

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: True

oauth_scope:
    - https://www.googleapis.com/auth/drive
    - https://www.googleapis.com/auth/drive.install

PPS-  I notice I get the exact same error if I do the `python quickstart.py` from the PyDrive2 docs -- the error occurs on the line where `gauth.LocalWebserverAuth()` is called.    Again, everything in my code used to work.  I do have `get_refresh_token: True` in `settings.yaml`, which is something [I saw you tell other people to check](https://github.com/iterative/PyDrive2/issues/92#issuecomment-788213806), so... Hmm. 

Update: Ok I notice that even though my credentials.json file is brand-new after running the scripts(s), it contains a field that reads "token_expiry": "2021-10-13T18:09:06Z". But I wasn't aware of having created any expired credentials. When I check my Drive API dashboard, my credentials don't say anything about an expiration date. (?)

drscotthawley commented 2 years ago

Ok, well, ultimately I just created a new OAuth Client ID and switched to that, and everything works again, no problems.
Wondering if I'll need to repeatedly create a new Client ID every month?

...in any case, feel free to close this issue for now if you like.