英文:
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 = ["email@sample.com"]; // email address the files or folders has been shared with
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"); //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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论