Google电子表格api请求的authentication范围不足

我正在使用nodejs上的以下脚本制作脚本以从Google电子表格中读取数据:

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

但在每一个请求我得到这个错误:

 Request had insufficient authentication scopes. 

我查看了Google开发者控制台,启用了Google Drive API,并启用了它,所以我不知道它会是什么。

范围看起来不错,也许您应该尝试删除先前存储在/Users/yourUserName/.credentials/sheets.googleapis.com-projectName/*中的凭据,然后再次执行该应用程序以获取新凭据。

首先,请确保您在Developers Console中启用了Sheets API

insufficient authentication scopes是由于请求中提供的OAuth 2.0令牌中的错误指定了访问所请求数据的范围。

因此,请确保您使用正确的和所有必要的范围,并且如果您正确执行此处的步骤, 请使用OAuth 2.0检查此授权请求 。

最后,尝试撤消访问并尝试重做。

有关更多信息,请检查相关的SO问题:

  • 用Java通过API写入GoogleSheet

  • Google Spreadsheets API与OAuth2.0使用Javascript

  1. 首先删除证书文件〜/ .credentials / sheets.googleapis.com-nodejs-quickstart.json(取决于您的设置)

  2. 更改用于从Google Spreadsheets中读取单元格的范围variables

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

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

  1. 执行代码后,API将再次进行validation,然后问题将被解决。