How to add a Google Drive folder to “My Drive” section to other users

◇◆丶佛笑我妖孽 提交于 2019-11-29 02:44:30
Henrique G. Abreu

This is very easy to do if each user could just access a link and authorize a script (that you build) to do the job for them (place a shared folder in their root folder).

But if it's a lot of users, you are the admin of the domain, and you really want to do it all automatically without anyone doing a thing, it is possible but probably very difficult to do. I mean, you need to access the Drive API directly and set oAuth 2.0 to impersonate your users, because the Apps Script built-in DocsList API does not have this impersonation feature. If you're really going for it, take a look at this other question.

First, set up a simple web app. The Google App Script editor even has a template that gets you most of the way there.

Second, implement something like the following and call it from the handler function.

function addRequiredFolders() {
  var root = DocsList.getRootFolder();
  var folderIds = ["somefolderid", "anotherfolderid"];
  folderIds.map( function(id) { DocsList.getFolderById(id).addToFolder(root) } );
}

I've tested a variant of this up to this point. The next step is to publish the Web App for your domain, and email it out to people or otherwise distribute it. I assume they will have the unpleasant step of needing to grant the web app permission to access their documents.

sergicurtu

Right now I've implemented this feature using "Google Documents List API". I know that this API is deprecated but for now it works.

(the code is not finished)

  (...)

  //var user = Session.getActiveUser().getUserLoginId()  OR
  var user = e.parameter.user_email 

  var TB_folder = e.parameter.TB_folder 
  var TB_sub_folder = e.parameter.TB_sub_folder 


  var base = 'https://docs.google.com/feeds/';
  var fetchArgs = googleOAuth_('docs', base);

  fetchArgs.method = 'POST';

  var rawXml =  "<?xml version='1.0' encoding='UTF-8'?>" + "<entry xmlns='http://www.w3.org/2005/Atom'>"
         +  "<category scheme='http://schemas.google.com/g/2005#kind' "
         +  "term='http://schemas.google.com/docs/2007#folder'/>"
         +  "<title>" + TB_folder +"</title>"
         +  "</entry>";

  fetchArgs.payload = rawXml;
  fetchArgs.contentType = 'application/atom+xml';
  fetchArgs.contentLength = 245;

 // POST a https://docs.google.com/feeds/default/private/full

 var url = base + user + '/private/full/?v=3&alt=json';
 var content = UrlFetchApp.fetch(url, fetchArgs).getContentText()
 var json = Utilities.jsonParse(content)


 var folder = json.entry.gd$resourceId.$t // -> I get "folder:folder_id"
 var id_folder = folder.split(':')[1]

 var folder_created_by = json.entry.gd$lastModifiedBy.email.$t
 var folder_owner      = json.entry.author['0'].email.$t

(...)

Now, you have the folder ID and can use it to create another subfolder or a file...

You need this function :

  //Google oAuth
  function googleOAuth_(name,scope) {
    var oAuthConfig = UrlFetchApp.addOAuthService(name);
    oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
    oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
    oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");

    oAuthConfig.setConsumerKey("XXXXXXXX");
    oAuthConfig.setConsumerSecret("XXXXXXXXXXXXXXXXXXX");

    //oAuthConfig.setConsumerKey("anonymous");
    //oAuthConfig.setConsumerSecret("anonymous");

    return {oAuthServiceName:name, oAuthUseToken:"always"};
  }     

You can create a file or add a user to a file (add a wriker).

Now I want to implement this functionality with " Drive API " . If someone has done it would be nice to get some help.

Sergi

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