【问题标题】:How to write to google finance API?如何写入谷歌金融 API?
【发布时间】:2018-08-16 21:26:08
【问题描述】:

我知道如何从谷歌金融 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");
      }
    });
  }

我做错了什么?我读过一些帖子,他们说他们没有添加资源,所以我尝试修复它,但仍然没有任何效果......

【问题讨论】:

  • 当你调用谷歌金融 api 时,这是你身边的一个问题。因为您没有传递满足该请求所需的身份验证凭据。
  • 您好,感谢您的回复,但问题是此时我还没有包含谷歌金融API,这只是我试图在谷歌表格上写一些东西

标签: reactjs react-native google-finance google-finance-api


【解决方案1】:

问题可能出在 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-01-27
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多