node.js Google 电子表格 api 请求的身份验证范围不足

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38534801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:26:07  来源:igfitidea点击:

Google spreadsheet api Request had insufficient authentication scopes

node.jsgoogle-apigoogle-spreadsheet-api

提问by jtomasrl

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

我正在制作一个脚本来使用 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.

我检查了 Google 开发人员控制台以启用 Google Drive API 并且它已启用,所以我真的不知道它会是什么。

回答by Long Nguyen

  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

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

  2. 更改用于从 Google 电子表格中读取单元格的范围变量

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

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

to

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

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.
  1. 代码执行后,API 将再次进行身份验证,然后问题将得到解决。

回答by Ken H

The scopes look good, maybe you should try to remove the credentials stored previously in /Users/yourUserName/.credentials/sheets.googleapis.com-projectName/*, and then execute the application again to get new credentials.

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

回答by KENdi

First, make sure you also enable the Sheets APIin your Developers Console.

首先,确保您还在开发者控制台中启用了 Sheets API

The insufficient authentication scopesis an error in the OAuth 2.0 token provided in the request specifies scopesthat are insufficient for accessing the requested data.

insufficient authentication scopes是误差在OAuth 2.0令牌在所述请求指定提供范围是不够用于访问所请求的数据。

So make sure you use the correct and all necessary scope and check this Authorizing requests with OAuth 2.0if you properly follow the steps here.

因此,如果您正确地按照此处的步骤操作,请确保使用正确且所有必要的范围,并检查此使用 OAuth 2.0 授权请求

Lastly, try to revoke the access and try to redo it.

最后,尝试撤销访问权限并尝试重做。

For more information, check this related SO question:

有关更多信息,请查看此相关 SO 问题:

回答by herrogokunaruto

In my case a token.pickle file was created in the same working directory as my program, I also had my credentials.json file in the same directory, all I did was deleted the token.pickle file , then changed the scope, then ran it again, it will again ask for authentication in your browser and that's it , it works.

在我的情况下,在与我的程序相同的工作目录中创建了一个 token.pickle 文件,我的credentials.json 文件也位于同一目录中,我所做的只是删除了 token.pickle 文件,然后更改了范围,然后运行再次,它会再次要求在您的浏览器中进行身份验证,就是这样,它可以工作。

My code snippet is like below , I was creating a pickle file so I had to delete it before changing scope

我的代码片段如下所示,我正在创建一个pickle文件,所以我必须在更改范围之前将其删除

    if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

回答by Hasan Sawan

Delete the token from tokens folder and try again. If you have tried to run the Google Drive Quickstart before running the Spreadsheet Quickstart, the existing token might not have the spreadsheet access updated.

从令牌文件夹中删除令牌并重试。如果您在运行电子表格快速入门之前尝试运行 Google 云端硬盘快速入门,则现有令牌可能没有更新电子表格访问权限。

回答by Mi.HTR

Also please check the installed version of googleapis in your workspace, because the latest version (51) does not support for Node 8. So you need downgrade its version. https://github.com/googleapis/google-api-nodejs-client/releases

另外请检查您工作区中已安装的 googleapis 版本,因为最新版本 (51) 不支持 Node 8。因此您需要降级其版本。 https://github.com/googleapis/google-api-nodejs-client/releases

回答by u4840403

The same problem occurred when I updated the scope. Along with the instruction, I delete the token.json file under the same directory with the python script, then the Authentication scope issue went away.

当我更新范围时发生了同样的问题。随着指令,我删除了与 python 脚本相同目录下的 token.json 文件,然后身份验证范围问题就消失了。

回答by Ajayvignesh

I was looking for the credential location for long time. Lot of forum mentioned that it'd be under c:\users\.credentials\google-api****.json But I couldn't find them anywhere. Then I figured out that my program created a token.picklefile right in the program .pydirectory which is essentially the credential file.

我一直在寻找凭证位置。很多论坛都提到它会在 c:\users\.credentials\google-api****.json 下,但我在任何地方都找不到它们。然后我发现我的程序在程序 .py目录中创建了一个token.pickle文件,该文件本质上是凭证文件。