How to get credentials for gmail API and OAuth 2.0 ?

Send emails securely with Gmail API

Introduction

To send emails securely through gmail with a web application, you need to use the Gmail API and OAuth 2. This blog will teach you how to obtain credentials for your project.

Google cloud platform

We must first register with our google account on the google cloud platform. So, head over to console.cloud.google.com where you can sign in / sign up. After doing so, you have to create a new project by clicking on the Select project button on the top navigation bar

pic1.png

Next, we can name our project and assign it a location. With that done, our new project will be created

pic3.png

First, we will set up our OAuth consent screen that can be found when hovering over the API and services button

pic14.png

Select the External radio button and then fill in details about your application. Leave the scopes and optional info page with the default configurations.

pic6.png

Creating credentials

We will now create our credentials that will eventually be used in our code for sending emails securely. Open the hamburger menu and hover over the API and services button. From there, select the credentials button to open the credentials page

pic4.png

Here, we will click on the Create credentials button and select the OAuth client ID option.

pic5.png

Select web application from the dropdown menu for the application type field. Then, name your application.

pic7.png

An important step is to scroll down and enter the following link in the redirect URI input: developers.google.com/oauthplayground.

pic8.png

Click on the create button

Now, you will have access to your client ID and client secret values. Make sure to copy and store them safely — they will be needed for the next step.

pic9.png

You must now navigate to developers.google.com/oauthplayground — the link you provided as the redirect URI earlier. Then, in the Select & Authorize APIs dropdown, input the following text: mail.google.com. In the settings button, tick the 'Use your own OAuth credentials' check box and enter the client ID and secret previously copied.

pic10.png

Click on the Authorize APIs button now.

pic11.png

If after selecting your gmail account you get the above error, return to the OAuth consent screen and add your own email ID as a test user.

Now, if you provide your app permission to read and compose emails, you will be returned to the OAuth playground page where an authorization code will be provided. Exchange this code for a refresh and access token.

Conclusion

Congratulations ! You have now received all the necessary credentials. Refer to the following blog to understand how you can use the data obtained to send emails securely:

aryaan.hashnode.dev/create-an-email-confirm..

Thank you for reading and happy coding !