I am trying to generate a report of all access permissions currently various users have in Team Drive. Is there any API in Appscript to Fetch this data?
- I don't believe team drives are supported in the DriveApp at this moment.Casper– Casper2018-03-27 08:28:35 +00:00Commented Mar 27, 2018 at 8:28
- I can see Powertools providing this report, so is there any other way I can fetch this data?Darpan Sanghavi– Darpan Sanghavi2018-03-27 10:16:26 +00:00Commented Mar 27, 2018 at 10:16
- @DarpanSanghavi yes, use the Drive API (i.e. the advanced service).tehhowch– tehhowch2018-03-27 11:25:15 +00:00Commented Mar 27, 2018 at 11:25
- Can you please provide me a refn link for this use case? I am aware with DriveAPI but could not find any refn for fetching file access rights for Team DriveDarpan Sanghavi– Darpan Sanghavi2018-03-27 12:22:51 +00:00Commented Mar 27, 2018 at 12:22
- You may want to check this developers.google.com/drive/v3/reference/permissions/listAnton Dementiev– Anton Dementiev2018-03-27 15:46:32 +00:00Commented Mar 27, 2018 at 15:46
2 Answers
There doesn't seem to be any method for getting Drive permissions sorted by username, so you will need to implement this business logic yourself. According to the documentation, sending a GET request to the API endpoint below will get you the list of permissions for the Team Drive (use Team Drive ID instead of file id):
https://www.googleapis.com/drive/v3/files/fileId/permissions
I don't have any Team Drives set up - the example below is based on getting permissions for a single file using Drive REST API. Before the code can be executed, you must prove your identity by including API key in URL parameters and passing OAuth token in the headers of your 'GET'request. The API key can be obtained from Google Cloud console. Enable the Drive API and click the key icon in the left menu to set up credentials. Choose "API key" from the drop-down and copy the value.
Your script must pass the token that includes all required authorization scopes to the API endpoint. OAuth scopes are set explicitly in the manifest file. In Script Editor, select "View - Show manifest file" and add relevant scopes. Scopes used in my manifest file are for accessing Drive Files and calling external services via UrlFetchApp:
"oauthScopes": [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request"] Finally, get the list of permissions for the file:
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); 6 Comments
Updates with help from Anton's answer. (Working with Appmaker as well).
Here's how I have achieved it,
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; }