Hello,

In this article, I will tell you how to use Gmail API. I am mostly using Gmail API in automation project to validate emails sent through my company’s system. We can login to Gmail and check the emails from UI but it takes a lot of time and it’s not a good practice to test Gmail in production. So we are using Gmail API. Let’s start

 

Enable Gmail API

First step is to enable the Google APIs and Service. So you need to go https://console.developers.google.com to activate the API access of the email that you will use in your project. Click ENABLE APIS AND SERVICES. It will take you to the search page.

There, you need to search for GMAIL.

When you select the GMAIL API, you need to push the ENABLE button. It’s so easy right!

Configure Gmail Consent Screen

Now you need to create credentials that you’ll use in the project. Those credentials will give you access to any Gmail services. Select CREATE CREDENTIALS button

 

In the next screen, you have to select the OAuth consent screen and select External. If you choose Internal, your app will require a verification. Select External and click CREATE button.

Give a name to your application. I added “SWTESTACADEMY” as my project name. There are some already defined Scopes for you. In case you need to do more with Gmail API, just add whatever suite your need. For this tutorial, default ones are ok.

Congrats, you just configured the consent screen that you will need once in the project.

 

Create GMAIL Credentials

Now, navigate to Credentials page once again. Select CREATE CREDENTIALS on the top menu. Then select OAuth client ID sub menu.

Choose Desktop App and give a name. I once again give the swtestacademy name. In case you are going to use Gmail in a different project you need to select a different app type like Android, TV app, etc..

Hit SAVE button. Here you go, your credentials are ready.

Now download the credentials by using the Download Icon next to the OAuth 2.0 Credentials. A JSON file will be downloaded. I will tell you how you are going to use this JSON.

Create a Java-Maven-Gradle Project

I create a Gradle project, you can change the dependencies according to your environment. Below dependencies are the only one I need for this tutorial.

First thing to do you need to read the json file you downloaded in the previous steps and use it to access to Gmail API. Here’s my project structure. I added the json file under the resources.

I have a string that hold the absolute location of the json file.

This json file is not enough to authenticate Gmail apis. You also need a StoredCredential file to authenticate. But this file will be created automatically in the same location as your json file. I will talk about it later. So you need another String that will hold this StoredCredential file.

Here’s the method that will authenticate Google. Those codes are almost exactly the same as in Gmail example that can be found in Google Developer Console documentation. We are going to use this method to access the Credential object.

Let’s get the total count of email that I have in my mail box. First two line are the default snippet to login the Gmail. So you don’t need to change anything. Then I use Gmail service and get the Threads sent to “me”. Now i have a List object with Thread. I can call the size method and get the total count of Threads.

Now, let’s check if I have an email with a given subject. First two lines are exactly the same. I get the email list sent to “me” and use setQ method to make a look up with a given input.

Now, i create a main class to trigger those methods.

Here’s the result.

Note: When you first execute your test, a browser will open. It will navigate to Gmail and you need to enter the credential you use to enable Gmail api and select OK for every questions that it asks. This is a one time thing.

Source Code: https://github.com/swtestacademy/gmail-tutorial

All you need to do is to add your credentials.json file into the project.