Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.
/ springboot-gmail Public archive

The goal of this project is to implement a simple Spring Boot Web Java application that communicates with a GMail inbox account using GMail API.

Notifications You must be signed in to change notification settings

ivangfr/springboot-gmail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

springboot-gmail

The goal of this project is to implement a Spring Boot Web Java application that communicates with a GMail inbox account using GMail API.

Important
For now, the project is frozen due to changes in Google API

Application endpoints

springboot-gmail Swagger

Prerequisites

Getting client_id, client_secret and refresh_token

  1. Access https://developers.google.com/gmail/api/quickstart/java and go to Step 1: Turn on the GMail API;

  2. Click on the blue button Enable the GMail API;

  3. A window with title Enable the Gmail API will open. In the field Enter new project name, you can keep the default Quickstart. Then, click on Next button;

  4. On the next window with title Configure your OAuth client, keep the Desktop app selected (default) and click on CREATE button;

  5. A window with title You’re all set! will open. Write down client_id and client_secret. They will be used on the next steps. Then, click on Done button;

  6. In order to receive a callback from Google, let’s start springboot-gmail application. So, open a terminal and inside springboot-gmail folder, run the following command

    ./mvnw clean spring-boot:run
  7. 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;

    Caution
    In 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
  8. Choose your Google account;

  9. A message will appear saying that Google hasn’t verified this app. Click on Advanced and then on Go to Quickstart (unsafe)

  10. Allow Quickstart to access your Google Account. You will get a code after completing all allowance steps. Write down this code. It will be used on the next step;

  11. Open a new terminal and run the following command, replacing CODE (obtained in the previous step), CLIENT_ID and CLIENT_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"
    }
  12. Done! Now we have all the credentials needed to access GMail account, i.e, client_id, client_secret and refresh_token;

  13. Shutdown the springboot-gmail application by pressing Ctrl+C.

Running application with Maven

  • In a terminal, make sure you are inside springboot-gmail root folder

  • Export the values of client_id, client_secret and refresh_token to their respective environment variable

    export 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

Testing

  • Get labels

    curl http://localhost:8080/api/labels
  • Get messages

    curl http://localhost:8080/api/messages
  • Get threads

    curl http://localhost:8080/api/threads

Shutdown

  • Go to the terminal where springboot-gmail application is running and press Ctrl+C

Disabling access to GMail API

  1. In a browser, access https://myaccount.google.com/permissions

  2. In Third-party apps with account access section, select Quickstart

    Third-party apps
  3. Finally, click on REMOVE ACCESS button.

    Remove Quickstart
  4. Click on OK to confirm the removal.

About

The goal of this project is to implement a simple Spring Boot Web Java application that communicates with a GMail inbox account using GMail API.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages