AWS Lambda - Pyhton - GMail API

This post includes:

  • use python to get labels from GMail API
  • deploy python package to AWS Lambda
  • setup a AWS gateway API to get labels

Get Labels from GMail API using Python

Reference: https://developers.google.com/gmail/api/quickstart/python

Create a working directory: ~/gmail-labels.
Follow the reference for credentials.json and install library.
Copy and Edit the code sample to become: ~/gmail-labels/main.py.

main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.labels']

def get_creds():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds

def get_service():
creds = get_creds()
service = build('gmail', 'v1', credentials=creds)
return service

def list(event, context):
# Call the Gmail API
service = get_service()
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)
# print("======= END of list =======")
return labels

def get_unreads(event, context):
service = get_service()
labels = list(None,None)[0:5]
labelToUnreadMessage = {}
for label in labels:
label_details = service.users().labels().get(userId='me', id=label['id']).execute();
labelToUnreadMessage[label['id']] = label_details['messagesUnread']
# print("label:", label_details)
print('labelToUnreadMessage: ', labelToUnreadMessage)
return labelToUnreadMessage

def get_unread(event, context):
id = event['id']
service = get_service()
label_details = service.users().labels().get(userId='me', id=id).execute();
result = {}
result['id'] = id
result['messagesUnread'] = label_details['messagesUnread']
print('result: ', result)
return result

# get_unread({'id':'INBOX'}, None)

Deploy python package to AWS Lambda

Install dependencies in the working directory:

1
2
cd ~/gmail-labels
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib -t .

in ~/gmail-labels, select all files(55) and compress to a Archive.zip (6M).

AWS Lambda Console

  • upload Archive.zip
  • change handler to : main.get_unread
  • save

test event

1
2
3
{
"id": "INBOX"
}

AWS Gateway API

  • add resource
  • add method
  • in Method Request, change API key required to true
  • in Integration Request, set mapping:
    • When there are no templates defined (recommended)
    • application/json
      1
      2
      3
      4
      #set($inputRoot = $input.path('$'))
      {
      "id": "$input.params('id')"
      }
  • test
  • deploy API to test stage, get URL

create a usage plan

API(test) <-> Usage Plan <-> API key(a customer)

PostMan

GET: https://xxtqwmuo0i.execute-api.us-east-2.amazonaws.com/test/labels?id=INBOX

Put API key to header: x-api-key

issues

Read-only file system: ‘token.pickle’

1
2
[Errno 30] Read-only file system: 'token.pickle'
with open('token.pickle', 'wb') as token

make change:

1
2
3
4
5
6
7
8
9
10
token_path = '/tmp/token.pickle'

# for offline use, token must put into tmp dir first
if not os.path.exists(token_path):
copyfile('token.pickle',token_path)

# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time.
with open(token_path, 'rb') as token:
creds = pickle.load(token)