Google spreadsheet api Request had insufficient authentication scopes

jtomasrl picture jtomasrl · Jul 22, 2016 · Viewed 71k times · Source

I'm making an script to read data from google spreadsheet using the following script on nodejs:

var url = oauth2Client.generateAuthUrl({
    access_type:     'offline',
    approval_prompt: 'force',
    scope: [
      'https://www.googleapis.com/auth/analytics.readonly',
      'https://www.googleapis.com/auth/drive',
      'https://www.googleapis.com/auth/spreadsheets'
    ]
});
global.googleapis = { expiry_date: 0 };
google.options({ auth: oauth2Client });
var sheets    = google.sheets('v4');
sheets.spreadsheets.get({ spreadsheetId: 'id'}, function(err, data) {
    res.send(err, data);
});

But on every get request i'm getting this error:

Request had insufficient authentication scopes.

I checked the Google developers console to enable the Google Drive API and it's enabled, so I dont really know what could it be.

Answer

Long Nguyen picture Long Nguyen · Oct 11, 2017
  1. Firstly delete the credentials files ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json (depending on your setting)

  2. Change the scope variable used for reading cells from Google Spreadsheets from

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];

to

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];

  1. After the execution of code, API will authenticate again and then the issue will be resolved.