Get Gmail data with Gmail API

How to do it 

Gmail API’s website tells us about how to send the http request to the gmail web application.

Flow of writing code

1. Obtain information from my gmail

First, I had to gain the authentication in order to send the http request to my account.

flow = client.flow_from_clientsecrets('credentials.json', 'https://www.googleapis.com/auth/gmail.readonly')
creds = tools.run_flow(flow, store)

This code provides us the authentication through our gmail account.

Second, I sent the http request to see the email list that is stored in my account.

service.users().messages().list(userId=user_id,q=query).execute()

“service.users().messages().list(userId=user_id,q=query)” creates the http request. “execute()” returns the json data. Viewing the json data, I got the message id that is needed.

Third, I got some message data in my account’s messages using message id.

These flow is written in this GitHub, which represents the code that provides us the information of the email that is sent to someone in the march 2019.

Application to my daily life

I started to do this thing because I want to calculate how long I worked in this month. In our part time job, we have to send the email after we worked. Working time information is stored in my gmail account. However, it takes time to gain work time information only. This is why I created this code.

Reference

Gmail API’s page: https://developers.google.com/gmail/api/

03/20/2019

シェアする

  • このエントリーをはてなブックマークに追加

フォローする