The goal of this project is to implement a Spring Boot
Web Java application that communicates with a GMail inbox account using GMail API
.
-
Access https://developers.google.com/gmail/api/quickstart/java and go to
Step 1: Turn on the GMail API
; -
Click on the blue button
Enable the GMail API
; -
A window with title
Enable the Gmail API
will open. In the fieldEnter new project name
, you can keep the defaultQuickstart
. Then, click onNext
button; -
On the next window with title
Configure your OAuth client
, keep theDesktop app
selected (default) and click onCREATE
button; -
A window with title
You’re all set!
will open. Write downclient_id
andclient_secret
. They will be used on the next steps. Then, click onDone
button; -
In order to receive a callback from Google, let’s start
springboot-gmail
application. So, open a terminal and insidespringboot-gmail
folder, run the following command./mvnw clean spring-boot:run
-
Open a browser and access the link below informing the
client_id
(obtained in the step 5.) and the scope that you allow the application to have;CautionIn this link, we are using the scope https://mail.google.com. It allows full access to the account! For more information about scopes, please check https://developers.google.com/gmail/api/auth/scopes. https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=CLIENT_ID&scope=https://mail.google.com&redirect_uri=http://localhost:8080/callback
-
Choose your Google account;
-
A message will appear saying that
Google hasn’t verified this app
. Click onAdvanced
and then onGo to Quickstart (unsafe)
-
Allow
Quickstart
to access your Google Account. You will get acode
after completing all allowance steps. Write down thiscode
. It will be used on the next step; -
Open a new terminal and run the following command, replacing
CODE
(obtained in the previous step),CLIENT_ID
andCLIENT_SECRET
(both obtained in the step 4.)curl -i -X POST https://www.googleapis.com/oauth2/v4/token \ -d "grant_type=authorization_code" \ -d "code=CODE" \ -d "client_id=CLIENT_ID" \ -d "client_secret=CLIENT_SECRET" \ -d "redirect_uri=http://localhost:8080/callback"
It should return something like
HTTP/2 200 ... { "access_token": "ya29.a0AfH6...v0cIbuQ", "expires_in": 3599, "refresh_token": "1//030V4mwr2...jLNKI", "scope": "https://mail.google.com/", "token_type": "Bearer" }
-
Done! Now we have all the credentials needed to access GMail account, i.e,
client_id
,client_secret
andrefresh_token
; -
Shutdown the
springboot-gmail
application by pressingCtrl+C
.
-
In a terminal, make sure you are inside
springboot-gmail
root folder -
Export the values of
client_id
,client_secret
andrefresh_token
to their respective environment variableexport GMAIL_CLIENT_ID=... export GMAIL_CLIENT_SECRET=... export GMAIL_REFRESH_TOKEN=...
-
Run the following command to start the application
./mvnw clean spring-boot:run
-
springboot-gmail
Swagger website can be accessed at http://localhost:8080/swagger-ui.html
-
Get labels
curl http://localhost:8080/api/labels
-
Get messages
curl http://localhost:8080/api/messages
-
Get threads
curl http://localhost:8080/api/threads
-
In a browser, access
https://myaccount.google.com/permissions
-
In
Third-party apps with account access
section, selectQuickstart
-
Finally, click on
REMOVE ACCESS
button. -
Click on
OK
to confirm the removal.