Accessing Files and Folders in Team Drive

こ雲淡風輕ζ 提交于 2019-12-02 07:50:29
Brian

After more research and digging, here's the solution for people with similar use cases.

  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.

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

  3. 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);
    }
}
francis

You can access but there's alot you can't do like removeFile() or getUrl() even when you have full access. You will still get the

{error: "Exception: Cannot use this operation on a Team Drive item."}

Workaround is to use setTrashed() instead of removeFile() on files/folders.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!