Generate Credentials File for Gmail API
You must generate a credentials file to authenticate with the Gmail API. For more information on the usage of the credentials file, see Credentials File.
Steps
To generate a credentials file for Gmail API, do the following:
Create a Project
You must create a project in the Google Cloud Console to manage APIs, permissions for Google Cloud resources, and more. For more information on creating projects in Google Cloud Console, see Create Projects.
Steps
To create a project in the Google Cloud Console, do the following:
Sign in to the Google Cloud Console.
Click Select a Project and click New Project.
Enter the project name, organization, and location.
Click Create.
Create a Service Account and Add Keys
You must create a service account and add keys to generate a credentials file. A service account is identified by its email address, which is unique to the account. For more information on service accounts, see Service Accounts.
Steps
To create a service account and add keys, do the following:
From the main menu, go to the API and Services > Credentials.
Click Create Credentials and select Service Account.
Enter service account details such as service account name, service account ID, and service account description.
Click Create and Continue.
(Optional) Grant the service account access to the project by selecting a role.
(Optional) Grant users access to this service account.
Go to Service Accounts and select the service account that you have created.
Go to the Keys tab and click Add Key.
Click Create New Key, select the key type as JSON, and click Create. Download the generated file since you cannot recover the credentials later.
Authorize Service Account to Access User's Data
As a super administrator, you must use domain-wide delegation to authorize the service accounts to access your users' data without requiring each user to give consent. For more information on domain-wide delegation, see Domain Wide Delegation.
Before you Start
Ensure that your Google Workspace account has super admin privileges.
Ensure that you have the client ID of the service account. You can find your service account's client ID in the service account page.
Steps
To authorize the service account to access the user's data, do the following:
Go to the domain administration panel and sign in with your super admin account.
From the main menu, go to Security > Access and data control and select API controls.
Click Manage Domain Wide Delegation and click Add New to add a new client ID.
Enter the client ID of the service account and enter
https://mail.google.com/
in the OAuth Scopes and click Authorize.