useDomainAdminAccess使用服务帐户身份验证在Google Drive API中创建错误

问题描述 投票:0回答:1

我正在创建一项服务,以查看我在Google云端硬盘上的某些共享云端硬盘文件夹。我正在使用服务帐户进行身份验证,因为这是在服务器上运行的服务。这是我的代码段:

const { google } = require("googleapis");

const auth = new google.auth.GoogleAuth({
  keyFile: "./credentials.json",
  scopes: ["https://www.googleapis.com/auth/drive"],
});

const drive = google.drive({ version: "v3", auth });

drive.drives
  .list({ q: "name = Clients", useDomainAdminAccess: true })
  .then((files) => {
    console.log(files);
  })
  .catch((err) => {
    console.log(err);
  });

如果我将list方法参数保留为空,则会返回状态代码200的响应,并且驱动器的列表空如预期(因为服务帐户不拥有任何共享驱动器)。添加use useDomainAdminAccess: true参数后,我将收到如下错误响应:

GaxiosError: Invalid Value
    at Gaxios._request (/Users/bteres/Projects/fw-docs/node_modules/gaxios/build/src/gaxios.js:85:23)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async JWT.requestAsync (/Users/bteres/Projects/fw-docs/node_modules/google-auth-library/build/src/auth/oauth2client.js:342:22) {
  response: {
    config: {
      url: 'https://www.googleapis.com/drive/v3/drives?q=name%20%3D%20Clients&useDomainAdminAccess=true',
      method: 'GET',
      userAgentDirectives: [Array],
      paramsSerializer: [Function],
      headers: [Object],
      params: [Object: null prototype],
      validateStatus: [Function],
      retry: true,
      responseType: 'json',
      retryConfig: [Object]
    },
    data: { error: [Object] },
    headers: {
      'alt-svc': 'h3-27=":443"; ma=2592000,h3-25=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
      'cache-control': 'private, max-age=0',
      connection: 'close',
      'content-encoding': 'gzip',
      'content-security-policy': "frame-ancestors 'self'",
      'content-type': 'application/json; charset=UTF-8',
      date: 'Sun, 07 Jun 2020 10:43:54 GMT',
      expires: 'Sun, 07 Jun 2020 10:43:54 GMT',
      server: 'GSE',
      'transfer-encoding': 'chunked',
      vary: 'Origin, X-Origin',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '1; mode=block'
    },
    status: 400,
    statusText: 'Bad Request',
    request: {
      responseURL: 'https://www.googleapis.com/drive/v3/drives?q=name%20%3D%20Clients&useDomainAdminAccess=true'
    }
  },
  config: {
    url: 'https://www.googleapis.com/drive/v3/drives?q=name%20%3D%20Clients&useDomainAdminAccess=true',
    method: 'GET',
    userAgentDirectives: [ [Object] ],
    paramsSerializer: [Function],
    headers: {
      'x-goog-api-client': 'gdcl/4.2.1 gl-node/12.17.0 auth/6.0.1',
      'Accept-Encoding': 'gzip',
      'User-Agent': 'google-api-nodejs-client/4.2.1 (gzip)',
      Authorization: 'REDACTED',
      Accept: 'application/json'
    },
    params: [Object: null prototype] {
      q: 'name = Clients',
      useDomainAdminAccess: true
    },
    validateStatus: [Function],
    retry: true,
    responseType: 'json',
    retryConfig: {
      currentRetryAttempt: 0,
      retry: 3,
      httpMethodsToRetry: [Array],
      noResponseRetries: 2,
      statusCodesToRetry: [Array]
    }
  },
  code: 400,
  errors: [
    {
      domain: 'global',
      reason: 'invalid',
      message: 'Invalid Value',
      locationType: 'parameter',
      location: 'q'
    }
  ]
}

我尝试将其省略,仅使用q参数,但得到相同的响应。如果我忽略了q参数,则会出现同样的问题。

我已经参加了几天,任何帮助将不胜感激。

UPDATE:我已经为服务帐户启用了域范围的委派,如下所示。Service account我还在我的G-Suite管理员API管理控件中启用了此功能,如下所示。G-Suite Admin API management我可能在这里得到一些不正确的配置吗?

google-drive-api google-drive-team-drive
1个回答
0
投票

[经过数小时的奋斗和轻推,我设法弄清了这一点。 Google的API文档对此不太清楚。基本上,您可以使用服务帐户,但是该服务帐户需要模拟特定的用户,即使您已完成所有配置,默认情况下也不是您域中的管理员用户。

所以我能想到的最简单的选择是使用google-auth的JWT凭据方法,如下所示:

const { google } = require("googleapis");

const credentials = require("./credentials.json");
const scopes = ["https://www.googleapis.com/auth/drive"];

const auth = new google.auth.JWT(
  credentials.client_email,
  null,
  credentials.private_key,
  scopes,
  "[email protected]"
);

const drive = google.drive({ version: "v3", auth });

将auth方法更改为此后,上面的查询按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.