访问 Team Drive 中的文件和文件夹 [英] Accessing Files and Folders in Team Drive
问题描述
我正在使用 Google Apps Script 和 Drive API 的 V2(我认为 V3 还没有在脚本中可用)在 Team Drive 中自动创建文件.我想用脚本添加编辑器,但没有成功.
I'm using Google Apps Script and V2 of the Drive API (I don't think V3 is available in scripts yet) to automate file creation inside of a Team Drive. I'd like to add editors with the script with no success.
我可以使用标准 DriveApp
方法中的 FolderIterator
访问团队云端硬盘和子文件夹.
I can access the Team Drive and child folders using the FolderIterator
in the standard DriveApp
methods.
尝试 1
function addUserToTeam(email, folders) {
// Open the team drive and get all the folders
var teamFolders = DriveApp.getFolderById('TEAMDRIVEIDSTRING').getFolders();
var folders = ["folderIdToMatch"] // This may hold multiple folders
try {
// Loop an array of folder IDs
for(var i=0; i<folders.length; i++) {
// Check the team drive folders for a matching name
while(teamFolders.hasNext()) {
var teamFolder = teamFolders.next();
if(folders[i] == teamFolder.getId()) {
teamFolder.addEditor(email);
}
}
}
} catch(e) {
Logger.log(e);
}
}
此操作失败,异常:无法对团队云端硬盘项目使用此操作
.
尝试 2
我通过将 teamFolder.addEditor(email)
替换为 Permissions
资源来尝试 Drive API:
I tried the Drive API by substituting teamFolder.addEditor(email)
a Permissions
resource:
if(folders[i] == teamFolder.getId()) {
var resource = {
"type":"user",
"role":"writer",
"value": email
}
Drive.Permissions.insert(resource, teamFolder.getId());
}
此操作失败并出现 File not found
错误.
This fails with a File not found
error.
我可以使用 DriveApp
方法找到文件夹(或文件).任何对 Drive API 的尝试都失败了.
I can find the folder (or file) with DriveApp
methods. Any attempt at the same with the Drive API fails.
我找不到任何说明无法通过 API 访问团队云端硬盘文件的文档.我的方法有问题吗?
I cannot find any documentation saying Team Drive files are inaccessible with the API. Is there something wrong with my approach?
推荐答案
经过更多的研究和挖掘,这里为有类似用例的人提供了解决方案.
After more research and digging, here's the solution for people with similar use cases.
您可以访问整个团队云端硬盘中的文件或云端硬盘中的文件,但不能访问文件夹.这样做是为了防止意外将敏感信息目录的访问权限授予不应访问的人.
You can access files to an entire Team Drive or to files inside the Drive, but not folders. This is done on purpose to prevent accidentally giving access to directories of sensitive information to people who shouldn't have access.
为了提供访问权限,supportsTeamDrives
是请求正文中的一个可选参数,它采用 boolean
值.将此设置为 true
并传入 API 调用.一个成功的函数如下.
To give access, supportsTeamDrives
is an optional argument in the request body that takes a boolean
value. Set this to true
and pass in the API call. A successful function is below.
实现我所描述的结果的唯一方法是使用多个团队云端硬盘并根据某些事件向用户授予访问权限.另一种选择是在项目期间将用户提升为完全权限(从编辑或查看),然后在完成后撤消.
The only way to achieve the outcome I described is to use multiple Team Drives and give access to users based on some event. Another option would be to promote a user to Full permissions (from edit or view) for the duration of the project and then revoke when completed.
将用户添加到团队云端硬盘
(这也适用于驱动器中的单个文件)
Add a user to a Team Drive
(This also works for single files in a Drive)
// Using Google Apps Script with v2 of the Drive API enabled
function addToTeamDrive() {
var resource = {
'value': emailString,
'type': 'user',
'role': 'writer'
}
// If you have several Team Drives, loop through and give access
try {
var TeamDrive = Drive.Teamdrives.list();
for(var i = 0; i < TeamDrive.items.length; i++) {
if(TeamDrive.items[i].name === "Team Drive String") {
// This ID may also be a single file inside a Team Drive
var id = TeamDrive.items[i].id;
}
}
// Add user permissions to the matched Drive
Drive.Permissions.insert(resource, id, {"supportsTeamDrives": true});
} catch(e) {
Logger.log(e);
}
}
这篇关于访问 Team Drive 中的文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!