Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 https://developers.google.com/gmail/api/quickstart/python#step_3_set_up_the_sample

And im using the following scopes :https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile

instead of this scope in the code below https://www.googleapis.com/auth/gmail.readonly&#39

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/quickstart.py", line 76, in <module>
main()
File "C:/Users//Gmail API intergration/quickstart.py", line 64, in main
results = service.users().labels().list(userId='me').execute()
File "C:\Python27\lib\site-packages\oauth2client\util.py", line 135, in positional_wrapper
return wrapped(*args, **kwargs)
File "build\bdist.win-amd64\egg\googleapiclient\http.py", line 832, in execute
googleapiclient.errors.HttpError:

Process finished with exit code 1



import httplib2
import os

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

try:
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
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 from storage.

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

Returns:
Credentials, the 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 = 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 = tools.run(flow, 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 = 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:')
for label in labels:
print(label['name'])


if __name__ == '__main__':
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

https://developers.google.com/oauthplayground

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.

Regards,
Faz

What I have tried:

I have tried google changing the code getting other code, using G+ API, but same thing happens..
Posted
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900