Javascript 为什么我的Google API访问密钥不起作用?

Javascript 为什么我的Google API访问密钥不起作用?,javascript,node.js,google-api,google-drive-api,google-api-nodejs-client,Javascript,Node.js,Google Api,Google Drive Api,Google Api Nodejs Client,我正试图通过API访问我的google drive文件。我正在使用google drive API代码,该代码是在中提供给我的,并对其进行了修改以满足我的需要。我需要访问令牌才能使用authorize()函数提供的auth参数激活这个listFiles()函数。我将把整个逻辑粘贴在这里,这样您就可以看到了 以下是我遇到的一些错误: (节点:6844)未处理的PromisejectionWarning:错误:无效的\u请求 在Gaxios。在Generator.next()处(节点:6844) 未

我正试图通过API访问我的google drive文件。我正在使用google drive API代码,该代码是在中提供给我的,并对其进行了修改以满足我的需要。我需要访问令牌才能使用authorize()函数提供的auth参数激活这个listFiles()函数。我将把整个逻辑粘贴在这里,这样您就可以看到了

以下是我遇到的一些错误:

(节点:6844)未处理的PromisejectionWarning:错误:无效的\u请求 在Gaxios。在Generator.next()处(节点:6844) 未处理的PromisejectionWarning:未处理的承诺拒绝。这 错误源于异步函数的内部抛出 没有拦截,或拒绝未处理的承诺 with.catch()。(拒绝id:1)

这是密码。我想在listFiles()函数中对drivefile进行console.log,并且我有一个token.json可从中读取。我不知道是什么问题

index.js:

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const { file } = require('googleapis/build/src/apis/file');

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive'];
// 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 a client with credentials, then call the Google Drive API.
  authorize(JSON.parse(content), listFiles());
});

/**
 * 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.web;
  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 getAccessToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    listFiles(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 getAccessToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'online',
    scope: SCOPES,
    response_type: 'token',
  });
  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);
    });
  });
}



/**
 * Lists the names and IDs of up to 10 files.
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */

async function listFiles(auth) {
  const drive = google.drive({ version: "v3", auth });
  const res = await drive.files.list({
    pageSize: 10,
    fields: "nextPageToken, files(id, name, description, mimeType, createdTime, parents, properties)",
  });
  const files = res.data.files;
  const fileArray = [];
  if (files.length) {
    const fileDisplay = [];
    const fileIdArray = [];
    const mimeType = [];
    const parents = [];
    const properties = [];
    console.log("Files:");
    for (var i = 0; i < files.length; i++) {
      fileDisplay.push(files[i].name);
      fileIdArray.push(files[i].id);
      mimeType.push(files[i].mimeType);
      properties.push(files[i].properties);
      parents.push(files[i].parents);
    }
    for (var y = 0; y < fileDisplay.length; y++) {
      fileArray.push({
        file: fileDisplay[y],
        id: fileIdArray[y],
        type: mimeType[y],
        parents: parents[y],
        properties: properties[y],
      });
    }
    app.set('fileArray', fileArray);
    console.log(fileArray)
  }
}

我真的需要一些帮助。我的客户感到不安。

你能看看网络选项卡上的请求和响应是什么样子的吗?并与我们分享,以帮助您?看起来上一个异步函数中的承诺被拒绝了,您能否尝试将wait语句包装在
try-catch
中,并记录您得到的错误?如果您查看这里的答案,就会发现与您的示例相当的工作代码。这两个区别是:(1)它使用刷新令牌而不是token.json;(2)它不使用任何库依赖项。在更改它之前,它是否工作?(1)您在哪一行收到错误?,(2)您对快速启动代码做了哪些更改以满足您的需要?,(3)删除
()
来自
listFiles
此处:
authorize(JSON.parse(content),listFiles()),(4)不应
列表文件(oAuth2Client)
be
callback(oAuth2Client)
?您能看看网络选项卡上的请求和响应是什么样子的吗?并与我们分享,以帮助您?看起来上一个异步函数中的承诺被拒绝了,您能否尝试将wait语句包装在
try-catch
中,并记录您得到的错误?如果您查看这里的答案,就会发现与您的示例相当的工作代码。这两个区别是:(1)它使用刷新令牌而不是token.json;(2)它不使用任何库依赖项。在更改它之前,它是否工作?(1)您在哪一行收到错误?,(2)您对快速启动代码做了哪些更改以满足您的需要?,(3)删除
()
来自
listFiles
此处:
authorize(JSON.parse(content),listFiles()),(4)不应
列表文件(oAuth2Client)
be
callback(oAuth2Client)
{"access_token":"***","scope":"https://www.googleapis.com/auth/drive","token_type":"Bearer","expiry_date":3599}