Integrating Google APIs with Node.js Applications
Google APIs is a set of application programming interfaces (APIs) developed by Google which allows communication with Google Services and their integration to other services. Examples of these include Gmail, Translate, Drive or Google Maps. Third-party apps can use these APIs to take advantage of or extend the functionality of the existing services.
Most of these have a common-auth process which I will explain in this blog
Pre-requisites
To use Google APIs with Node.js first install googleapis library in your project directory by running.
npm install googleapis --saveSecond head over to Google Cloud Platform and create a new project. Now from the hamburger menu select API & Services.

Click “Enable APIs and Service” below the search bar. This should open the API Library. Now search for the APIs you want to use in your application and then click enable.

Now head over to the Credentials section and click the Create Credentials button below the search bar. Once you have created the credentials, download the client-secret.json file and save it in your project directory.
Now in your project directory create an Auth.js file and paste the below code. Don’t worry, it’s a well-commented code. 😏
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly','https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose','https://www.googleapis.com/auth/gmail.send'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if(err){
return console.log('Error loading client secret file:', err);
}
// Authorize the client with credentials, then call the Gmail API.
authorize(JSON.parse(content), getAuth);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if(err){
return getNewToken(oAuth2Client, callback);
}
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function getAuth(auth){
}
The important thing to note here is that the SCOPES array stores all the permissions your application needs. For example, if you want to send mails from your application paste the link needed from official GMAIL API Docs in the SCOPES array. The same implies to other Google APIs like Drive and Calendar.
When you run the code for the first time, you will have to follow the link that shows up in the terminal. Allow the application the permissions it needs.
You will now get a key, paste it into the terminal, and hit enter which will then generate the tokens.json file and an auth object.
Now there are two things to note here:
Your application is now connected with Google APIs.