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

前端 未结 3 684
终归单人心
终归单人心 2020-12-16 08:10

I have a Google Drive herarchichal folder and subfolders structure owned by me and I want to add it to the \"My Drive\" section in all users in our Google Apps for Business

相关标签:
3条回答
  • 2020-12-16 08:43

    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.

    0 讨论(0)
  • 2020-12-16 08:45

    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

    0 讨论(0)
  • 2020-12-16 09:07

    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.

    0 讨论(0)
提交回复
热议问题