0

我知道如何从谷歌金融 api 中读取,这很简单。但是当我尝试编写时,出现以下错误:

错误:请求的身份验证范围不足

这是我的代码:

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

// If modifying these scopes, delete token.json.
const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.
fs.readFile('./GoogleFinanceApi/credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  // Authorize a client with credentials, then call the Google Sheets API.
  authorize(JSON.parse(content), appendData);
});

这里追加数据中的 ^ 是我调用函数的地方,它在我执行 listMajors 时有效,但在我执行 appendData 时无效...

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

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getNewToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

function listMajors(auth) {
  const sheets = google.sheets({version: 'v4', auth});
  sheets.spreadsheets.values.get({
    spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
    range: 'Sheet1!A2:E',
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const rows = res.data.values;
    if (rows.length) {
      console.log('Name, Major:');
      // Print columns A and E, which correspond to indices 0 and 4.
      rows.map((row) => {
        console.log(`${row[0]}, ${row[4]}`);
      });
    } else {
      console.log('No data found.');
    }
  });
}
function appendData(auth) {
    var sheets = google.sheets('v4');
    sheets.spreadsheets.values.append({
      auth: auth,
      spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
      range: 'Sheet1!A2:B', //Change Sheet1 if your worksheet's name is something else
      valueInputOption: "USER_ENTERED",
      resource: {
        values: [ ["Void", "Canvas", "Website"], ["Paul", "Shan", "Human"] ]
      }
    }, (err, response) => {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      } else {
          console.log("Appended");
      }
    });
  }

我究竟做错了什么?我读过一些帖子,他们说他们没有添加资源,所以我试图解决这个问题,但仍然没有任何效果......

4

1 回答 1

0

可能问题出在 appendData 的 google.sheets 中。也许您需要在访问工作表之前将身份验证传递给 google.sheets,就像您在 listMajors 中所做的那样,但是您将身份验证传递给工作表而不是 google.sheets。这可能是个问题

你可以试试下面更新的代码

function appendData(auth) {
    const sheets = google.sheets({version: 'v4', auth})
    sheets.spreadsheets.values.append({
      spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
      range: 'Sheet1!A2:B', //Change Sheet1 if your worksheet's name is something else
      valueInputOption: "USER_ENTERED",
      resource: {
        values: [ ["Void", "Canvas", "Website"], ["Paul", "Shan", "Human"] ]
      }
    }, (err, response) => {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      } else {
          console.log("Appended");
      }
    });
  }
于 2018-08-17T05:42:42.040 回答