当前位置: 首页 > 知识库问答 >
问题:

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

甄鹏云
2023-03-14

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

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的响应,以及预期的空驱动器列表(因为服务帐户不拥有任何共享驱动器)。当我添加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参数,同样的问题也会出现。

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

共有1个答案

曾山
2023-03-14

经过几个小时的挣扎和另一次轻推,我终于找到了答案。谷歌的API文档对此并不清楚。基本上,你可以使用你的服务帐户,但服务帐户需要模拟一个特定的用户,默认情况下,它不是你域中的管理员用户,即使你已经完成了所有配置。

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

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方法更改为该方法后,上面的查询按预期工作。

 类似资料:
  • 我不知道我的代码或Firebase控制台有什么问题,但当我运行应用程序并单击提交按钮尝试创建一个帐户时,它会运行,我不知道为什么。 很抱歉对我的描述含糊不清,但如果有人能告诉我我做错了什么,我将非常感激。 } 错误消息

  • 我的团队目前正在开发一个应用程序,使用Admin SDK在GCP中列出公司的域用户,用于入职和非入职目的。 我们使用一个服务帐户来完成这个任务,并且在Google Admin's advanced settings中添加了 作用域。管理SDK API被激活,我们可以在凭据区域中看到服务帐户。 当我们使用参数 和 调用https://www.googleapis.com/admin/director

  • 我试图利用Google Drive API将我们的应用程序创建的日志文件发送到我们公司的Google Drive帐户,我们为Android开发共享该帐户。大多数示例展示了如何使用oAuth2与最终用户的Google Drive帐户进行身份验证,但我们希望文件只发送到我们的Google Drive帐户。 经过一些搜索,我发现我应该为我们的应用程序创建一个API项目,并使用与该API项目相关联的服务帐

  • 我正在处理一个使用PHP的项目,需要使用PHP客户端库实现Google CloudAPI,但身份验证似乎对我不起作用。我已经创建了一个服务帐户并授予了项目所有者权限,我不想通过使用GOOGLE_DEFAULT_CREDENTIALS环境变量进行身份验证,我想使用服务帐户身份验证。 致命错误:未捕获异常“Google\Cloud\Core\exception\ServiceException”,消息

  • 问题内容: 我正在尝试访问Googles Contacts API,但由于获得授权而失败。从其他(网络)语言开始,我习惯于使用API​​Console和公共API密钥(授权)。 这样,我不能够刷新令牌并不能确定如何使用公共密钥的accessToken ......相反,我尝试了一个服务帐户: 这是我的例外: 谢谢提示! 问题答案: 不调用我的代码就可以很好地工作。但是您只会拥有一个模拟帐户(ser

  • 问题内容: 我想在Jenkins管道中使用,因此必须首先使用Google Service帐户进行身份验证。我正在使用https://wiki.jenkins.io/display/JENKINS/Google+OAuth+Plugin,其中包含我的私钥凭据。我一直坚持将凭据加载到管道中: 我也尝试过from档案,但是没有运气。 日志显示: 问题答案: 您需要将您的“服务帐户” JSON文件作为 机