Read and modify emails programmatically via Gmail API Clients

Haris Saleem
4 min readMay 21, 2019

--

Source: AndroidPolice

In the context of Automated tests, at some point, we all have had to deal with emails. Be it reading a support request launched from within the app, retrieving a One Time Password from your email to log in or sign up, or testing a successful sign up.

There is a handy library in Java called JavaMail which allows programmatically accessing and managing emails quite easily. Google also has its own Client API for Gmail which exposes APIs to read and modify emails in a provided Gmail account. It is more secure than JavaMail as it uses OAuth 2.0 and more reliable as it is owned by Google. This API has Client libraries in various major languages like Java, Python, .Net, Ruby etc

In this article, I will explain how we can access emails using Gmail API by using its Ruby Client.

Start by heading on over to Google Developer Console, search and enable the Gmail API:

As suggested, create Credentials to be able to access the API:

Select Gmail API and User data as we will be accessing emails.

Click “What credentials do I need?”, enter a name for your Client, Create and download the OAuth Client ID

Once credentials JSON file is downloaded, press Done, you will be taken back to the Credentials page where the newly created credential will also be listed.

Now, we need to create an OAuth consent using ‘OAuth consent screen’ tab. This is necessary as here you configure that consent has to be given in order to access the private user data.

Add an application name and support email.

Depending on what operation you will be performing, select the API scopes you need. For example, if you just need to read emails, you can use ../auth/gmail.readonly scope. Click save and now we are set to access the API.

In order to be able to use the Gmail API, we need to download and install the google-api-client gem, run the following in a terminal:

gem install google-api-client

One done installing, open following link and copy the quickstart.rb (you can select programming language from the sidebar) — there are very easy instructions on the page.

Place the quickstart.rb and credentials.json in a folder, open quickstart.rb in your favourite editor/IDE. Replace CREDENTIALS_PATH with name of your credentials JSON file and Application name that you provided in credentials screen.

APPLICATION_NAME = 'test_app'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze

Replace user_id value with the email address that you want to access.

user_id = 'testemail@gmail.com'

Run the program by executing following command in a terminal:

ruby quickstart.rb

IMPORTANT: When running for the first time, you will be asked to sign in to the google account that you are trying to access, during this process, an access token is saved into token.yaml file if the authentication is successful. Every time afterwards, you will not need any manual intervention unless token is tampered with in token.yaml

The quickstart.rb file has simple logic already built in to retrieve and display labels in your email account. Let’s try something more practical — like extracting subject for a given email:

Notice that you can provide metadata_headers to reduce the size of your response and therefore making it faster.

You can try out all API operations before automating for example, try retrieving list of drafts here.

Similarly, you can perform many operations, depending on the scope.

SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_MODIFY

For example, moving email(s) to trash, changing labels, sending emails, changing READ and UNREAD status etc require specific values in for scope, you can find a list of available scopes here.

A very detailed reference of available methods in Ruby client can be found here. For other languages, refer to this link.

So there you have it. if you found this information useful, inaccurate or lacking, or have questions, do not hesitate to leave comments.

--

--

Haris Saleem

Hi! I am Haris, I am a professional Software Tester with expertise in Test Automation. I like travelling, photography, painting and meeting new people.