【问题标题】:Getting an error while trying receive Gmail from Node.js using Google official library尝试使用 Google 官方库从 Node.js 接收 Gmail 时出错
【发布时间】:2020-08-04 05:34:12
【问题描述】:

这是我试过的代码,

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

如何调试?

【问题讨论】:

标签: node.js gmail-api


【解决方案1】:

您必须转到 GCP 并转到您创建的项目。点击添加 API 访问权限,将 Gmail API 添加到当前项目。

参考:

【讨论】:

猜你喜欢
  • 2021-02-24
  • 2016-11-16
  • 1970-01-01
  • 2015-07-27
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
相关资源
最近更新 更多