Unified.to
Blog

Setting up OAuth2 credentials for Google Mail


June 25, 2024

Introduction

OAuth2 is a popular authentication method used by many web services including Google. To access Google services such as GMail with the Unified API, you need to set up OAuth2 credentials in Google Cloud Console.

Steps to obtain OAuth2 credentials for GMail

  1. Create or Select a Project on Google Cloud Console
    Go to Google Cloud Console and select an existing project or create a new project. If new, name it as you wish and click on "Create".
  2. Enable Gmail API
    Once the project is created, go to the "Library" in the left-hand side menu, search for "Gmail API" and enable it.
  3. Create Credentials
    After enabling the Gmail API, go to the "Credentials" tab on the left-hand side menu. Click on "Create Credentials" and choose "OAuth client ID".
  4. Configure OAuth Consent Screen
    Before creating the credentials, you'll be asked to configure the OAuth Consent Screen. Fill the necessary details and save it.
  5. Create OAuth client ID
    Now you can create your OAuth client ID. Choose "Web application" as the application type. You can name it as you wish.
    For "Authorized redirect URIs", enter the URI where you want the authentication response to be redirected. Click "Create" after filling the details.
  6. Get your Credentials
    After the OAuth client ID is created, you can get your credentials. It will include a "Client ID" and a "Client Secret". Note them down.

Now, you have your OAuth2 credentials ready to authenticate and access GMail API.

Warning

Make sure that your OAuth2 Reauthentication policy is set correctly.

  1. In the Admin console, go to Menu > Security > Access and data control > Google Cloud session control.
  2. On the left, select the organizational unit where you want to set session length. For all users, select the top-level organizational unit. Initially, an organizational unit inherits the settings of its parent.
  3. Under Reauthentication policy, select Require reauthentication, and choose the Reauthentication frequency from the drop-down list.
  4. Set it to "Never require reauthentication"
Blog