0

I know how to read from the google finance api, it is pretty simple. But when I try to write I get the following error:

Error: Request had insufficient authentication scopes

This is my code:

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

Here ^ in the append data is where I am calling the function, it works when i do the listMajors but not when I do the 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");
      }
    });
  }

What am I doing wrong? I have read some posts and they say they didn't add the resource so I tried to fix that but still nothing works...

  • It’s an issue in your side when you are calling google finance api. Because you are not passing required auth credentials that is required to fulfil that request. – Hemadri Dasari Aug 17 '18 at 02:51
  • Hi, thank you for the response, but the problem is that at this moment I still have not included the google finance API, this is just me trying to write something to the google sheet – Jose Montalvo Aug 17 '18 at 05:33

1 Answers1

0

Probably the issue is in google.sheets in appendData. Perhaps you need to pass auth to google.sheets before you access sheets as how you are doing in listMajors but you are passing auth to the sheets instead of google.sheets. This might be an issue

Can you try below updated code

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");
      }
    });
  }
Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162