Appscript API用于获取团队驱动器的访问权限

时间:2018-03-27 05:10:36

标签: google-apps-script google-app-maker

我正在尝试生成当前各种用户在Team Drive中拥有的所有访问权限的报告。 Appscript中是否有任何API来获取此数据?

2 个答案:

答案 0 :(得分:1)

似乎没有任何方法可以按用户名排序驱动器权限,因此您需要自己实现此业务逻辑。根据文档,向下面的API端点发送GET请求将获得Team Drive的权限列表(使用Team Drive ID而不是文件ID):

  

https://www.googleapis.com/drive/v3/files/fileId/permissions

我没有设置任何Team Drives - 下面的示例基于使用Drive REST API获取单个文件的权限。在执行代码之前,您必须通过在URL参数中包含API密钥并在“GET'request”的标头中传递OAuth令牌来证明您的身份。 API密钥可以从Google Cloud控制台获取。启用Drive API并单击左侧菜单中的密钥图标以设置凭据。从下拉列表中选择“API密钥”并复制该值。

您的脚本必须将包含所有必需授权范围的令牌传递给API端点。 OAuth作用域在清单文件中明确设置。在“脚本编辑器”中,选择“查看 - 显示清单文件”并添加相关范围。我的清单文件中使用的范围用于访问驱动器文件并通过UrlFetchApp调用外部服务:

 "oauthScopes": [
  "https://www.googleapis.com/auth/drive",
  "https://www.googleapis.com/auth/script.external_request"]

最后,获取文件的权限列表:

  var fileId = "FILE_ID";
    var apiKey = "API_KEY";

    var apiUrl = "https://www.googleapis.com/drive/v3/files/fileId/permissions";

    var token = ScriptApp.getOAuthToken();
    var header = {"Authorization":"Bearer " + token};

    var options = {
    "method":"GET",
    "headers": header,
    "muteHttpExceptions": true
    };

    var res = UrlFetchApp.fetch(apiUrl.replace("fileId", fileId) + "?key=" + apiKey, options)
                         .getContentText();

        var permissions = JSON.parse(res);
        Logger.log(permissions);

答案 1 :(得分:1)

Anton 答案的帮助下更新。 (也与Appmaker合作)。

这是我如何实现它,

function fileExport(folderId) //pass folder id or drive/team drive id to fetch permissions
{
    var parent = DriveApp.getFolderById(folderId);
    var path = DriveApp.getFolderById(folderId).getName();
    var fileName = 'Permisssions_' + new Date(); //define file name
    var newExport = SpreadsheetApp.create(fileName); // create new spreadsheet
    var header = ["Path","Folder","File Name","Email","Role","Name","DocUrl"]; //define header
    newExport.appendRow(header); // append header to spreadsheet
    newExport.setFrozenRows(1);
    newExport.getRange("A1:H1").setFontWeight("bold");

    //traverse through each folder under current folder
    getChildFolders(parent,newExport,path);

     //appned files associated with current folder
    var files = parent.getFiles();

    while (files.hasNext()) {
      var file = files.next();
      var permitFile=  makeRestCall(file.getId());  

      for(var j=0; j<permitFile.length;j++)
      {
          newExport.appendRow([path,'',file.getName(),permitFile[j].emailAddress, permitFile[j].role,permitFile[j].displayName,file.getUrl()]);
      }
     }
    return 'File exported successfully to this path:'+ newExport.getUrl(); 
}
//Iterate through child folders using recursive call
function getChildFolders(parent,newExport,path) {

  var childFolders = parent.getFolders();

  while (childFolders.hasNext()) {

    var childFolder = childFolders.next();
    path = path +'--'+childFolder.getName();
    var permit=  makeRestCall(childFolder.getId());
    for(var i=0; i<permit.length;i++)
    {
        newExport.appendRow([path,childFolder.getName(),'',permit[i].emailAddress, permit[i].role,permit[i].displayName,childFolder.getUrl()]);
    }

    var files = childFolder.getFiles();

    while (files.hasNext()) {
      var file = files.next();
      var permitFile=  makeRestCall(file.getId());  

      for(var j=0; j<permitFile.length;j++)
      {
          newExport.appendRow([path,'',file.getName(),permitFile[j].emailAddress, permitFile[j].role,permitFile[j].displayName,file.getUrl()]);
      }
    }
    // Recursive call for any sub-folders
    getChildFolders(childFolder,newExport,path);    
  }
}

function makeRestCall(fileOrFolderId)  //make rest call to fetch permissions
{
  var apiUrl = "https://www.googleapis.com/drive/v3/files/fileId/permissions";

  var token = ScriptApp.getOAuthToken();
  var header = {"Authorization":"Bearer " + token};

  var options = {
    "method":"GET",
    "headers": header
  };
  var response = UrlFetchApp.fetch(apiUrl.replace("fileId", fileOrFolderId) + "?supportsTeamDrives=true&fields=*", options)
  .getContentText();
  var dataAll = JSON.parse(response); 
  var permit = dataAll.permissions;
  return permit;
}