json - Getting KeyError using Gmail API with Python -




this code copied page: https://developers.google.com/gmail/api/quickstart/python

from __future__ import print_function import httplib2 import os  apiclient import discovery oauth2client import client oauth2client import tools oauth2client.file import storage  try:     import argparse     flags = argparse.argumentparser(parents=[tools.argparser]).parse_args() except importerror:     flags = none  # if modifying these scopes, delete saved credentials # @ ~/.credentials/gmail-python-quickstart.json scopes = 'https://www.googleapis.com/auth/gmail.readonly' client_secret_file = 'client_secret.json' application_name = 'gmail api python quickstart'   def get_credentials():     """gets valid user credentials storage.      if nothing has been stored, or if stored credentials invalid,     oauth2 flow completed obtain new credentials.      returns:         credentials, obtained credential.     """     home_dir = os.path.expanduser('~')     credential_dir = os.path.join(home_dir, '.credentials')     if not os.path.exists(credential_dir):         os.makedirs(credential_dir)     credential_path = os.path.join(credential_dir,                                    'gmail-python-quickstart.json')      store = storage(credential_path)     credentials = store.get()     if not credentials or credentials.invalid:         flow = client.flow_from_clientsecrets(client_secret_file, scopes)         flow.user_agent = application_name         if flags:             credentials = tools.run_flow(flow, store, flags)         else: # needed compatibility python 2.6             credentials = tools.run(flow, store)         print('storing credentials ' + credential_path)     return credentials  def main():     """shows basic usage of gmail api.      creates gmail api service object , outputs list of label names     of user's gmail account.     """     credentials = get_credentials()     http = credentials.authorize(httplib2.http())     service = discovery.build('gmail', 'v1', http=http)      results = service.users().labels().list(userid='me').execute()     labels = results.get('labels', [])      if not labels:         print('no labels found.')     else:       print('labels:')       label in labels:         print(label['name'] + ": " + label['messagestotal'])   if __name__ == '__main__':     main() 

this line changed:

        print(label['name'] + ": " + label['messagestotal']) 

in order display label , corresponding total number of messages per documentation here: https://developers.google.com/gmail/api/v1/reference/users/labels

but when try run python3, error:

traceback (most recent call last):   file "quickstart.py", line 74, in <module>     main()   file "quickstart.py", line 70, in main     print(label['name'] + ": " + label['messagestotal']) keyerror: 'messagestotal' 

i'm pretty new python , using json objects links these also.

here related so post faced same error.

keyerror means key not exist.

here example given in post.

>>> mydict = {'a':'1','b':'2'} >>> mydict['a'] '1' >>> mydict['c'] traceback (most recent call last):   file "<stdin>", line 1, in <module> keyerror: 'c' >>> 

so, try print content of meta_entry , check whether path exists or not.

>>> mydict = {'a':'1','b':'2'} >>> print mydict {'a': '1', 'b': '2'} 

or, can do:

>>> 'a' in mydict true >>> 'c' in mydict false 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -