如何查找Google Drive中一个电子邮件地址可以访问的所有文件?

huangapple go评论68阅读模式
英文:

How can I find all files in Google Drive that an email address can access?

问题

我有一堆分发列表和Google群组。在Google Drive中,我们已经授予这些分发列表和Google群组对文件/文件夹的访问权限。

有没有办法找出这些分发列表和Google群组可以访问Google Drive中的哪些文件/文件夹?

英文:

I have a slew of distribution lists and Google Groups. In Google Drive, we have given access to files/folders to these distribution lists and Google Groups.

Is there a way I can find what files/folders in Google Drive these Dls and Google Groups can access?

答案1

得分: 2

以下是代码部分的中文翻译:

我假设您将查询由您的用户拥有的文件如果是这种情况我强烈建议使用以下代码这是由[Tanaike][1][这个问题][2]创建的答案

function myFunction() {
   const checkEmailAddresses = ["email@sample.com"]; // 文件或文件夹共享的电子邮件地址

  let files = [];
  let pageToken = "";
  do {
    const obj = Drive.Files.list({fields: "items(id,title,mimeType,shared,permissions(emailAddress)),nextPageToken", q: "'me' in owners and visibility='limited' and trashed=false", maxResults: 1000, pageToken});
    files = files.concat(obj.items);
    pageToken = obj.nextPageToken;
  } while (pageToken);
  let res = files.filter(({shared}) => shared);
  if (checkEmailAddresses && checkEmailAddresses.length > 0) {
    res = res.filter(({permissions}) => permissions.some(({emailAddress}) => checkEmailAddresses.includes(emailAddress)));
  }
  const values = res.map(({title, id, mimeType}) => [title, id, mimeType]);
  values.unshift(["title", "id", "mimeType"]);
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1"); // 设置表格名称。
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

请注意,此代码将文件名、ID和元数据类型打印到电子表格中,不要忘记将Drive API高级服务添加到您的项目中(项目必须与Google表格绑定才能工作)。

如果您使用的是企业帐户(即Google Workspace),并且要检查组织中的所有文件,则需要执行用户模拟。总体逻辑相同,但需要模拟每个用户并列出他们拥有的文件,以便获取权限。

参考资料:


<details>
<summary>英文:</summary>

I’m assuming that you will be querying files owned by your user, if that’s the case, I highly recommend using the following code that was created by [Tanaike][1] as an answer for [this question][2]: 

```js
function myFunction() {
   const checkEmailAddresses = [&quot;email@sample.com&quot;]; // email address the files or folders has been shared with

  let files = [];
  let pageToken = &quot;&quot;;
  do {
    const obj = Drive.Files.list({fields: &quot;items(id,title,mimeType,shared,permissions(emailAddress)),nextPageToken&quot;, q: &quot;&#39;me&#39; in owners and visibility=&#39;limited&#39; and trashed=false&quot;, maxResults: 1000, pageToken});
    files = files.concat(obj.items);
    pageToken = obj.nextPageToken;
  } while (pageToken);
  let res = files.filter(({shared}) =&gt; shared);
  if (checkEmailAddresses &amp;&amp; checkEmailAddresses.length &gt; 0) {
    res = res.filter(({permissions}) =&gt; permissions.some(({emailAddress}) =&gt; checkEmailAddresses.includes(emailAddress)));
  }
  const values = res.map(({title, id, mimeType}) =&gt; [title, id, mimeType]);
  values.unshift([&quot;title&quot;, &quot;id&quot;, &quot;mimeType&quot;]);
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&quot;1&quot;); //Set the sheet name.
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
} 

The code is printing the file name, ID and metadata type into a spreadsheet, don’t forget to add the Drive API advance service into your project (the project must be bound to a Google Sheet to work).

If you are working with a business account (aka Google Workspace), and you want to check all files in the organization; you will need to perform user impersonation. The overall logic is the same, but impersonating each user and list each of the files owned by them so you can acquire the permissions.

References

huangapple
  • 本文由 发表于 2023年6月8日 03:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定