Google drive api useDomainAdminAccess使用服务帐户身份验证在Google驱动器API中创建错误

Google drive api useDomainAdminAccess使用服务帐户身份验证在Google驱动器API中创建错误,google-drive-api,google-drive-shared-drive,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"], }); co

我正在创建一个服务来观看我在谷歌硬盘上的一些共享硬盘文件夹。我使用服务帐户进行身份验证,因为这是一个在服务器上运行的服务。以下是我的代码片段:

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);
  });
如果我将
列表
方法参数保留为空,我将返回一个状态代码为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
参数,同样的问题也会出现

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

更新: 我已经为服务帐户启用了域范围的委派,如下所示。 我也在我的G-Suite管理API管理控件中启用了此功能,如下所示。
我是不是有些配置不正确?

经过几个小时的挣扎和另一次轻推,我终于找到了答案。谷歌的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方法更改为此后,上面的查询按预期工作。

经过数小时的努力和另一次轻推,我终于找到了答案。谷歌的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方法更改为该方法后,上面的查询将按预期工作。

我认为问题可能与使用服务帐户有关。如文档
useDomainAdminAccess
中所述,标记为所述域管理员的帐户应使用该访问权限。尝试将此权限授予服务帐户,或者使用您知道是admin的帐户执行相同的请求,然后返回查看是否有效?否则,您的代码似乎完全正确感谢@Raserhin的响应。我已经更新了问题,以显示我在域范围的委派设置方面已经做了哪些工作。我可能做错什么了吗?当我尝试使用OAuth和我的帐户(超级管理员)时,我得到了整个域的结果,但服务帐户仍然没有结果。我认为问题可能与使用服务帐户有关。如文档
useDomainAdminAccess
中所述,标记为所述域管理员的帐户应使用该访问权限。尝试将此权限授予服务帐户,或者使用您知道是admin的帐户执行相同的请求,然后返回查看是否有效?否则,您的代码似乎完全正确感谢@Raserhin的响应。我已经更新了问题,以显示我在域范围的委派设置方面已经做了哪些工作。我可能做错什么了吗?当我尝试使用OAuth和我的帐户(超级管理员)时,我得到了整个域的结果,但服务帐户仍然没有结果。