iterative / PyDrive2

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

PyDrive GAuth Error #261

Closed phen1 closed 1 year ago

phen1 commented 1 year ago

I got this error when I try to use this Pydrive2

/Library/Python/3.8/lib/python/site-packages/oauth2client/client.py", line 302, in new_from_json
module_name = data['_module']
KeyError: '_module'

And this is my code:

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LoadCredentialsFile('/Users/phenlot/Documents/client_secrets.json')

gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)
shcheklein commented 1 year ago

The regular Auth flow doesn't imply / require running gauth.LoadCredentialsFile(). It's more of an internal, advanced call that expects different credentials.

One way is to have a settings.yaml, client_secrets.json that you downloaded from GCP:

client_config_backend: file
client_config_file: client_secrets.json

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.file
  - https://www.googleapis.com/auth/drive.install
  - https://www.googleapis.com/auth/drive.readonly

and use code like this:

import os
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

Option 2 is to pass settings.yaml as a dict into GoogleAuth(). Similar to this example:

    settings = {
                "client_config_backend": "file",
                ....
            }
    # Create instance of GoogleAuth
    gauth = GoogleAuth(settings=settings)
    # Authenticate
    gauth.ServiceAuth()
    return gauth

Option 3 is similar to option 2, but you can pass client config also as part of that dict. The full config can be found here - https://docs.iterative.ai/PyDrive2/oauth/. See the These are all the possible fields of a settings.yaml file: part.