Ошибка при попытке получить Gmail от Node.js с помощью официальной библиотеки Google - PullRequest
0 голосов
/ 21 апреля 2020

Это код, который я пробовал,

"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);
};

и получение ошибки

(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)

Как я могу отладить это?

1 Ответ

2 голосов
/ 21 апреля 2020

Вам потребуется go для GCP и go для проекта, который вы создали. Нажмите Добавить доступ к API и добавьте API Gmail в текущий проект.

enter image description here

enter image description here

Справка:

...