from apiclient.discovery import build from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage from oauth2client.tools import run
# Path to the client_secret.json file downloaded from the Developer Console CLIENT_SECRET_FILE = 'client_secret.json'
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
# Location of the credentials storage file STORAGE = Storage('gmail.storage')
# Start the OAuth flow to retrieve credentials flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE) http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them credentials = STORAGE.get() if credentials is None or credentials.invalid: credentials = run(flow, STORAGE, http=http)
# Authorize the httplib2.Http object with our credentials http = credentials.authorize(http)
# Build the Gmail service from discovery gmail_service = build('gmail', 'v1', http=http)
# Retrieve a page of threads threads = gmail_service.users().threads().list(userId='me').execute()
# Print ID for each thread if threads['threads']: for thread in threads['threads']: print 'Thread ID: %s' % (thread['id'])
from apiclient.discovery import build from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage from oauth2client.tools import run from apiclient import errors import base64 import email
# Path to the client_secret.json file downloaded from the Developer Console CLIENT_SECRET_FILE = 'client_secret.json'
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
# Location of the credentials storage file STORAGE = Storage('gmail.storage')
# Start the OAuth flow to retrieve credentials flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE) http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them credentials = STORAGE.get() if credentials is None or credentials.invalid: credentials = run(flow, STORAGE, http=http)
# Authorize the httplib2.Http object with our credentials http = credentials.authorize(http)
# Build the Gmail service from discovery gmail_service = build('gmail', 'v1', http=http)
# Retrieve a page of threads # threads = gmail_service.users().threads().list(userId='me').execute()
# # Print ID for each thread # if threads['threads']: # for thread in threads['threads']: # print 'Thread ID: %s' % (thread['id'])
def ListMessagesWithLabels(service, user_id, label_ids=[]): """List all Messages of the user's mailbox with label_ids applied.
Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. label_ids: Only return Messages with these labelIds applied.
Returns: List of Messages that have all required Labels applied. Note that the returned list contains Message IDs, you must use get with the appropriate id to get the details of a Message. """ try: response = service.users().messages().list(userId=user_id, labelIds=label_ids).execute() messages = [] if 'messages' in response: messages.extend(response['messages'])
while 'nextPageToken' in response: page_token = response['nextPageToken'] response = service.users().messages().list(userId=user_id, labelIds=label_ids, pageToken=page_token).execute() messages.extend(response['messages'])
def GetMessage(service, user_id, msg_id): """Get a Message with given ID.
Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. msg_id: The ID of the Message required.
Returns: A Message. """ try: message = service.users().messages().get(userId=user_id, id=msg_id).execute()
a = ListMessagesWithLabels(gmail_service,'me')[0]['id'] b = GetMessage(gmail_service,'me',a) print b['snippet'] print b['payload']['headers'][3]['value']