Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API quickstart.py error KeyError: '_module'

Using Gmail API. My client secret file is downloaded and working for Ruby. When I try the quickstart.py (python) version I get this error

File "quickstart.py", line 70, in <module>
    main()
  File "quickstart.py", line 55, in main
    credentials = get_credentials()
  File "quickstart.py", line 38, in get_credentials
    credentials = store.get()
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 374, in get
    return self.locked_get()
  File "/Library/Python/2.7/site-packages/oauth2client/file.py", line 79, in locked_get
    credentials = Credentials.new_from_json(content)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 281, in new_from_json
    module = data['_module']
KeyError: '_module'

I have not changed the file at all, just added the client_secret.json to that working directory and also install the google-api-python-client. My python code came from here: https://developers.google.com/gmail/api/quickstart/python

like image 703
ElioRubens Avatar asked Jun 21 '15 16:06

ElioRubens


4 Answers

oauth2client is trying to load credentials from a json file with an incorrect structure.

Maybe the Ruby client uses a different file format but I'd be surprised. Are you sure you didn't save client_secret.json as ~/.credentials/gmail-quickstart.json accidentally?

Regardless, removing ~/.credentials/gmail-quickstart.json and re-authenticating will generate a new credentials file with the correct structure.

like image 180
Matt Avatar answered Nov 06 '22 07:11

Matt


Try replacing creds = store.get() with creds = None temporarily. If this works, you can refactor your code to always start with flow-based credentials instantiation. This worked for me. It seems Google samples are out of sync with their oauth2client.

like image 12
skirill Avatar answered Nov 06 '22 08:11

skirill


I am learning Python myself and had a similar problem, but with the Calendar API example. It turned out that it was a typo with regards to the SCOPE.

## Typo - Invalid definition
SCOPES = 'https://ww.googleapies.com/auth/calendar.readonly'
## Correct Value for SCOPE
SCOPES = 'https://www.googleapis.com/auth/calendar'

Also, Matt's answer help point me in the right direction. The gmail-quickstart.json is not the same thing as the client_secret.json. The client_secret.json allows you to make a request for an OAuth2 token. While the gmail-quickstart.json contains the issued token and meta-data associated with it. The gmail-quickstart.json isn't created until you successfully login.

One last thought, in order to log in successfully, the quickstart.py app launched an instance of my web-browser (Firefox) and went to the Google login screen. In order for Firefox to run properly, I had to set my DISPLAY variable properly first.

$ export DISPLAY=:0
$ xhost +
access control disabled, clients can connect from any host
like image 2
Randy B. Avatar answered Nov 06 '22 07:11

Randy B.


In this GitHub issue: error "KeyError: '_module'" when running gdrive_upload.py

sputnik-dev answared on 10 Jan 2016:

If someone have the same issue : auth_token.txt and client_secret.json are not the same! Don't link the client_secret.json from google API console. The file will be automatically created by the script.

Wrong way: gauth.SaveCredentialsFile("client_secret.json")

Right way: gauth.SaveCredentialsFile("<any random name>.json")

Extra:

PyDrive code that automate google drive api authetication. Use the browser just one time to authenticate and never more. It saves your credential data on mycreds.json :)

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.json")

if gauth.credentials is None:
    gauth.LocalWebserverAuth()

elif gauth.access_token_expired:
    gauth.Refresh()

else:
    gauth.Authorize()

gauth.SaveCredentialsFile("mycreds.json")
like image 2
Danilo Matrangolo Marano Avatar answered Nov 06 '22 09:11

Danilo Matrangolo Marano