【问题标题】:Get all Drive files across all users获取所有用户的所有云端硬盘文件
【发布时间】:2017-09-19 23:15:32
【问题描述】:

我有一个使用域范围委派的服务帐户设置。我想通过一次调用搜索所有用户的文件。

我在 Google Apps 脚本中使用 Drive API 和 OAuth2 项目来处理身份验证。似乎您在拨打电话时总是必须指定要模拟的用户(下面的代码)。当我删除 setSubject 方法时,我没有得到任何结果。

有没有办法做到这一点?

  OAuth2.createService('GoogleDrive:' + userEmail)
  // Set the endpoint URL.
  .setTokenUrl('https://accounts.google.com/o/oauth2/token')

  // Set the private key and issuer.
  .setPrivateKey(PRIVATE_KEY)
  .setIssuer(CLIENT_EMAIL)

  // Set the name of the user to impersonate. This will only work for
  // Google Apps for Work/EDU accounts whose admin has setup domain-wide
  // delegation:
  // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
  .setSubject(userEmail)

  // Set the property store where authorized tokens should be persisted.
  .setPropertyStore(PropertiesService.getScriptProperties())

  // Set the scope. This must match one of the scopes configured during the
  // setup of domain-wide delegation.
  .setScope('https://www.googleapis.com/auth/drive');

谢谢

【问题讨论】:

  • 您可以将服务帐户设置与域范围委派一起使用。正如thread 中所述,您应该使用 Google Drive API 并使用服务帐户来执行Google Apps domain-wide delegation of authority
  • 基于此documentation,您需要返回一个FileIterator,它允许脚本遍历可能很大的文件集合。这是一个示例code
  • @abielita 抱歉,我不够具体。我已经通过服务帐户使用 Drive API。我在上面的帖子中留下了更多细节。谢谢

标签: google-apps-script


【解决方案1】:

您可以尝试Drive API,它可以通过Advanced Drive Service 从应用脚本访问。与 DriveApp 相比,它具有附加功能,并允许您使用搜索查询。您还可以使用API Explorer 来测试您的请求。例如,将查询字符串从Advanced Drive Service 页面复制到API Explorerq 字段中。该查询字符串是"root" in parents and trashed = false and mimeType = "application/vnd.google-apps.folder"。如果您然后单击执行,您将看到结果。查询字符串的语法解释为here

【讨论】:

  • 谢谢@SpiderPig。对不起,我不够具体。当我使用 Drive API 时,我已经用更多信息更新了帖子,但如果我不冒充用户,我不会得到任何结果。所以想知道如何在不冒充用户的情况下拨打电话,以便我可以在 1 次通话中搜索所有文件。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 2018-12-28
  • 2021-12-04
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多