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

Google spreadsheet api Request had insufficient authentication scopes

我正在制作脚本以在 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 驱动器 API 并且它已启用,所以我真的不知道它会是什么。

首先,请确保您还在开发者控制台中启用了工作表API

请求中提供的 OAuth 2.0 令牌中的 insufficient authentication scopes 错误指定 scopes 不足以访问所请求的数据。

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

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

有关详细信息,请查看此相关的 SO 问题:

  • Google Spreadsheets API with OAuth2.0 using Javascript

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

  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'];

  1. 执行完代码后,API会重新认证,问题就解决了。

更新范围时出现同样的问题。根据说明,我删除了与 python 脚本位于同一目录下的 token.json 文件,然后身份验证范围问题就消失了。

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

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

我的代码片段如下所示,我正在创建一个 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)

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

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

如@herrogokunaruto 所述,确保 token.pickle 文件不在工作区文件夹中

就我而言 (Java + IntellijIdea) 我删除了一个文件:~/IdeaProjects/projectName/tokens/StoredCredential 并重新启动了代码。

此文件夹是使用 GoogleAuthorizationCodeFlow.Builder 创建的,因此可能有所不同,但文件应该相同。

重新启动后,我再次切换到快速启动页面以进行访问管理 - 为用户提供所有访问权限并按照。

之后,对我来说一切都很好

Select 当 OAuth 弹出窗口打开询问 google 帐户权限

时,授予对 google 工作表读取、编辑等权限的适当权限