0

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?

9
  • I don't believe team drives are supported in the DriveApp at this moment. Commented Mar 27, 2018 at 8:28
  • I can see Powertools providing this report, so is there any other way I can fetch this data? Commented Mar 27, 2018 at 10:16
  • @DarpanSanghavi yes, use the Drive API (i.e. the advanced service). Commented 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 Drive Commented Mar 27, 2018 at 12:22
  • You may want to check this developers.google.com/drive/v3/reference/permissions/list Commented Mar 27, 2018 at 15:46

2 Answers 2

1

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); 
Sign up to request clarification or add additional context in comments.

6 Comments

Any particular reason why you don't use the native client library / advanced service, and prefer instead UrlFetchApp?
The reason was the one you mentioned above - this method uses version 3 of Drive API that Advanced Drive doesn't seem to support developers.google.com/drive/v3/reference/permissions/list Maybe it does, but I have no idea if it supports passing Team Drive ids as well as file ids.
apps script uses v2 :)
Anton: I will try the solution and keep you updated. Appreciate the help.
Tehhowch : I have tried using advance service,but as I said Drive SDK does not return me any method which can give me permission in team drive. If you can provide me any method name/ snippet that will help.
|
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.