Subha
Subha

Reputation: 23

Getting an error while trying receive Gmail from Node.js using Google official library

This is the code which I tried,

"use strict";
const fs = require("fs");
const { promisify } = require("util");
const { google } = require("googleapis");
const { OAuth2Client } = require("google-auth-library");
const gmail = google.gmail("v1");
// Promisify with promise
const readFileAsync = promisify(fs.readFile);
// Gmail label list
const gmailListLabesAsync = promisify(gmail.users.labels.list);
const TOKEN_DIR = __dirname;
const TOKEN_PATH = TOKEN_DIR + "/gmail-nodejs-quickstart.json"; 
// Specify the access token file
const main = async () => {
// Get credential information
const content = await readFileAsync(__dirname + "/client_secret.json"); 
// specify the client secret file
const credentials = JSON.parse(content); 
// credential
// authentication
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);
const token = await readFileAsync(TOKEN_PATH);
oauth2Client.credentials = JSON.parse(token);
// Access the sheet via API
const response = await gmailListLabesAsync({
auth: oauth2Client,
userId: "me",});
// display the result
console.log(response.data);
};

and getting error

(node:8261) UnhandledPromiseRejectionWarning: Error: Access Not Configured. Gmail API has not been used in project 85650736890 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=85650736890 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
    at Gaxios._request (/NODEGMAIL/node_modules/gaxios/build/src/gaxios.js:85:23)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async OAuth2Client.requestAsync 
(NODEGMAIL/node_modules/google-auth-library/build/src/auth/oauth2client.js:340:18)
(node:8261) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

How can I debug this?

Upvotes: 0

Views: 195

Answers (1)

Algo7
Algo7

Reputation: 2166

You'll have to go to GCP and go to the project you created. Click add API access and add the Gmail API to the current project.

enter image description here

enter image description here

Reference:

Upvotes: 2

Related Questions