Click here to Skip to main content
14,239,882 members
Rate this:
Please Sign up or sign in to vote.
Hello guys,

1st post here! YAY :)

Im Really NOOB with API'S and cant get any Working code that helps :( I dont ask much, just for community support and direction :P

To the point, Im struggling with Googles API. all i want is the code to work and im stuck now for 4 days on a piece of code that I got from google. Im using the GMAIL API reason why is I really want to pull the User info (Name,Surname,Age and Email) from the gmail address that the user types in. but also I want to use the API to write emails in the program that im writing (if the user as any queries) Baby steps 1st.

Im running the following code in Python :
Link to the code

And im using the following scopes :

instead of this scope in the code below

But the problem is if i run it it gives me the following problems.....
Traceback (most recent call last):
File "C:/Users/Gmail API intergration/", line 76, in <module>
File "C:/Users//Gmail API intergration/", line 64, in main
results = service.users().labels().list(userId='me').execute()
File "C:\Python27\lib\site-packages\oauth2client\", line 135, in positional_wrapper
return wrapped(*args, **kwargs)
File "build\\egg\googleapiclient\", line 832, in execute

Process finished with exit code 1

import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/gmail-python-quickstart.json
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'

def get_credentials():
"""Gets valid user credentials from storage.

If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.

Credentials, the obtained credential.
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
credential_path = os.path.join(credential_dir,

store = oauth2client.file.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 only for compatibility with Python 2.6
credentials =, store)
print('Storing credentials to ' + credential_path)
return credentials

def main():
"""Shows basic usage of the Gmail API.

Creates a Gmail API service object and outputs a list of label names
of the user's Gmail account.
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service ='gmail', 'v1', http=http)

results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])

if not labels:
print('No labels found.')
for label in labels:

if __name__ == '__main__':

Now im sitting here and pulling my grey hairs out because they are getting more and more by each day... :(

I have looked at this code but... its not working for me. used the

And im getting what i need there with the scopes im using.. but otherwise no luck

Please can someone point me in the right direction.


What I have tried:

I have tried google changing the code getting other code, using G+ API, but same thing happens..
Richard MacCutchan 2-Jun-16 3:46am
You need to look at lines 76 and 64 in your code to ee what they are doing. The messages imply that something is not working correctly, or causing an exception. Use try/catch to capture any exception details and see if you can identify what it is complaining about. You also need to add <pre> tags around your code, so it is readable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100