-
Notifications
You must be signed in to change notification settings - Fork 4
/
examples.py
47 lines (37 loc) · 1.52 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Examples of how to use the Gmail API
How-To (quickstart-python)
https://developers.google.com/gmail/api/quickstart/quickstart-python
API documentation for GMAIL Api
https://developers.google.com/gmail/api/v1/reference/
Documentation for python wrapper for GMAIL Api
https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/gmail_v1.users.html
"""
import authenticator
from pprint import pprint
gmail_service = authenticator.authenticate_gmail_service()
# Print ID for each thread
threads = gmail_service.users().threads().list(userId='me').execute()
if threads['threads']:
for thread in threads['threads']:
print 'Thread ID: %s' % (thread['id'])
# Print labels
labels = gmail_service.users().labels().list(userId='me').execute()
for label in labels['labels']:
print label
# Print particular label
promotions_label = gmail_service.users().labels().get(userId='me', id='CATEGORY_PROMOTIONS').execute()
inbox_label = gmail_service.users().labels().get(userId='me', id='INBOX').execute()
pprint(inbox_label)
# Print one page of messages
messages = gmail_service.users().messages().list(userId='me').execute()
pprint(messages)
# Print entire messages list
request = gmail_service.users().messages().list(userId='me')
response = request.execute()
messages = response['messages']
while response.get('nextPageToken'):
request = gmail_service.users().messages().list_next(previous_request=request, previous_response=response)
response = request.execute()
messages += response['messages']
pprint(messages)