访问Team Drive中的文件和文件夹 [英] Accessing Files and Folders in Team Drive

查看:197
本文介绍了访问Team Drive中的文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Apps脚本和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访问Team Drive和子文件夹.

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);
  }
}

此操作失败,并显示Exception: Cannot use this operation on a Team Drive item.

尝试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​​访问Team Drive文件.我的方法有问题吗?

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.

  1. 您可以访问整个Team Drive的文件,也可以访问Drive中的文件,而不能访问文件夹中的文件.这样做是为了防止意外将敏感信息目录授予不应该访问的人.

  1. 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.

将用户添加到Team Drive

(这也适用于云端硬盘中的单个文件)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆