首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >https://www.googleapis.com/oauth2/v4/token API错误: FetchError:请求GoogleSheet失败,原因: getaddrinfo ENOTFOUND 808

https://www.googleapis.com/oauth2/v4/token API错误: FetchError:请求GoogleSheet失败,原因: getaddrinfo ENOTFOUND 808
EN

Stack Overflow用户
提问于 2021-07-22 11:25:59
回答 1查看 1.5K关注 0票数 0

我正在尝试访问,但首先我尝试使用下面的代码生成访问令牌。

代码语言:javascript
运行
复制
const { google } = require("googleapis");

getAccessToken = () => {
  return new Promise(function(resolve, reject) {
   const key = require('./credentials.json');
   const jwtClient = new google.auth.JWT(
    key.client_email,
    null,
    key.private_key,
    "https://www.googleapis.com/auth/spreadsheets",
    null
   );
   jwtClient.authorize(function(err, tokens) {
    if (err) {
     reject(err);
     return;
    }
    console.log("token===",tokens.access_token);
    resolve(tokens.access_token);
   });
  });
 };

getAccessToken();

但未能在jwtClient.authorize()上授权它,并获得了如下错误:

代码语言:javascript
运行
复制
C:\Google API\GoogleSheet UsingJWT\GoogleSheet UsingJWT>node index.js
(node:16820) UnhandledPromiseRejectionWarning: FetchError: request to https://www.googleapis.com/oauth2/v4/token failed, reason: getaddrinfo ENOTFOUND 808
    at ClientRequest.<anonymous> (C:\Google API\GoogleSheet UsingJWT\GoogleSheet UsingJWT\node_modules\node-fetch\lib\index.js:1461:11)
    at ClientRequest.emit (events.js:314:20)
    at onerror (C:\Google API\GoogleSheet UsingJWT\GoogleSheet UsingJWT\node_modules\agent-base\dist\src\index.js:117:21)
    at callbackError (C:\Google API\GoogleSheet UsingJWT\GoogleSheet UsingJWT\node_modules\agent-base\dist\src\index.js:136:17)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16820) 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:16820) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我也不使用任何代理。我无法理解实际问题。

,有人能帮我吗?

EN

Stack Overflow用户

发布于 2021-08-11 14:39:25

我知道您希望为您的Sheets Node.js脚本生成一个Node.js,但是您得到了一个作为响应的错误。该错误消息描述此问题是如何从未处理的承诺中产生的。要解决这种情况,您应该使用已处理的承诺。这样做的一种方法是使用以下示例:

代码语言:javascript
运行
复制
const fs = require('fs');
const readline = require('readline');
const {
  google
} = require('googleapis');

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
const TOKEN_PATH = 'token.json';

fs.readFile('credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  authorize(JSON.parse(content), myFunction);
});

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]);

  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getNewToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

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 while trying to retrieve access token', err);
      oAuth2Client.setCredentials(token);
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) return console.error(err);
        console.log('Token stored to', TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}

function myFunction(auth) {
  // YOUR CODE HERE
}

如所述,该脚本将达到您的目标,您只需记住更改作用域 (如果需要)并更新myFunction。如果您需要这种方法的帮助,可以随意放弃评论。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68483997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档