首页 > 解决方案 > useDomainAdminAccess 使用服务帐户身份验证在 Google Drive API 中创建错误

问题描述

我正在创建一项服务来查看我在 Google Drive 上的一些 Shared Drive 文件夹。我正在使用服务帐户进行身份验证,因为这是在服务器上运行的服务。这是我的代码片段:

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 和空驱动器列表的响应(因为服务帐户不拥有任何共享驱动器)。添加 useuseDomainAdminAccess: 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参数,同样的问题。

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

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

标签: google-drive-apigoogle-drive-shared-drive

解决方案


经过数小时的挣扎和另一次轻推,我设法弄清楚了这一点。谷歌的 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,
  "admin@domain.com"
);

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

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


推荐阅读