Use the node gmail API to access your mailbox
The difficult part to use the Google Gmail API is not to use the API itself, but to set up your project to have the proper authorization tokens.
This API relies on the oAuth2 protocol to grant access to your gmail account, and the end goal is to have an access token
, and a refresh token
to access your gmail account.
The OAuth2 terminology may seems confusing at first but the protocol makes more sense when you understand the different parties involved in the process:
- resource owner: that’s you. You own the gmail account and you need at some point to manually authorize the access to your mail box
- client: this is the application which wants to access your email account. In the context of a CLI node application, this is not a real application per se - there is for instance no user interface for that application, but this is still an entity that you need to create.
- scope: a scope is the mechanism by which you limit the permissions you grant the client. For instance, you may want to give the client only the permission to read your emails but not to send one. During the authorization process, you, the resource owner, will be presented with the list of permissions that the client is asking so that you can review them before granting access
- authorization server: this is the server that you will connect to where you, the resource owner, will grant access to your gmail account. This server is hosted by Google and you can trust that server with your credentials.
- resource server: this is also a Google server which will be the server giving access to your mail box, and that will check your access token to confirm that you have the rights to perform the operations.
Create the client application
In the google documentation page, there is a button labeled “Enable the Gmail API” which does exactly that, but we will describe here the steps which are performed behind the scene.
This will make it easier, in a subsequent article, to describe hot to add the pub/sub API.
You first need to log into the Google Cloud Platform dashboard at: https://console.developers.google.com/apis/dashboard
Create a Google Cloud project
This Dashboard will have a “Select a project” menu which will allow you to create an application:
select that menu, and create a new project:
Enable the Gmail API for your project
The application will now appear in the console, and will tell you how to enable APIs:
You want to add the Gmail API:
and ENABLE it
Create credentials for your Gmail API
As prompted at the top of the screen, you need to get some credentials:
This step is a bit confusing.
The confusing part is that you need to click that What credentials do I need?
button to move forward:
Setup the Gmail conset screen for your application
you click on the “SET UP CONSENT SCREEN” button
If you have a private gmail account, the only available option is External
:
and finally, you have this long form where you can provide more information about the application/client.
The Add scope
button allows you to list the permissions that you want for your application.
Note: we’ll see that, when programmatically getting the tokens, you also need to provide the scopes in the authorization API, so it is not totally clear if it is required to set the scopes here in the form.
The form will display warnings about the fact that your app is not verified and, as described in that document , there will be restrictions associated to this application.
The good news is that this is not an issue for what we want to do, which is to get tokens for a CLI application.
Download your credentials
You then go back to your dashboard, in the “Credentials” section to create credentials:
You need to select the OAuth client ID
option:
This will prompt you with this screen:
and, finally, you will get the Client ID and secret that you need to start using the Google Node API.
Back on the dashboard/credentials page, you will be able to download those credentials:
Save those credentials in a file called credentials.json
You will need this file in the next step.
Using the google node authorization API
The credentials you collected before must be kept secret: they will be used to collect the access tokens which will allow your program to access your mailbox.
The google node API documentation here shows a program to list the labels of your mail box.
The problem with that example is that it does 2 things at once:
- get the access and refresh tokens
- use those tokens to use the gmail API
I split the program in 3 parts to make it easier to reuse:
- a program to get the access tokens, through the authentication URL, and store those tokens ina
token.json
file - a utility function which generate a OAuth2Client to authenticate any API call
- 2 examples of programs using the GMail API (one to get the list of lables, the other one to send an email), using the utility described above
Get an access token and a refresh token
Initialize the oAuth api
In the code below, credentials.installed
is the object read from your credentials.json
file
Get the authorization URL
You, as the owner of the mailbox, need to grant access to the API. To do so you need an authorization URL:
the access_type is offline
because we will be calling the API from the command line.
SCOPES
is an array containing the permissions you want for the API calls.
The list of scopes for the gmail API is available here.
If you check the API reference, you will see, for each API, the scopes which are required to call that API.
For instance, if you want to read the labels of your mailbox, the documentation will show you the scopes needed:
For the scope (I know…) of this demo, we will use those scopes:
- https://www.googleapis.com/auth/gmail.labels
- https://www.googleapis.com/auth/gmail.send
- https://www.googleapis.com/auth/gmail.readonly
As usual, the rule of thumb is to request for the minimal set of scopes to limit the security exposure of your application.
The code of the program to access the token is a small modification of the example provided by Google where I removed the code which was actually calling the gmail API:
When you run the program:
It will display the authorization URL.
Get an authorization code using the authorization URL
When accessing that URL in your browser, you hit the authorization server which asks you which gmail account to use or to sign
but when you select your mail, you get…
This was kind of expected with all the warnings that we had when creating the app above. As this page is not user facing in our use case, we can bypass this warning by clicking the advanced button:
and click on the bottom link:
This link will show the permissions that the app is asking for so that, as the owner of the mailbox can verify and grant:
The counter at the bottom of the page (1/3) tells you that the app is requesting 3 permissions and you can selectively grant or deny those permissions
and finally
You then have a final confirmation screen:
The last screen will give you the authorization code:
You can copy the value of this authorization code in the prompt of your command and press enter:
The API will exchange this authorization code for your tokens and store them in your token.json
file
Using the google gmail API using the tokens
Once you have the token.json
file (that you also absolutely need to keep secret!) you can use the gmail API and call the APIs that are allowed by the scopes you requested.
You can have a reusable function which will give you access to an oAut2Client:
Retrieve the list of labels
The code below will retrieve the list of labels associated to your mailbox (error checking code omitted for brevity):
Send an email
the following code will send an email on ‘your’ behalf (error checking code omitted for brevity).
List of emails
the following code will list the messages currently in your mail box (pagination code not implemented)
Enjoy!