Add an writer to a spreadsheet via “Google Document List API”

后端 未结 2 657
长情又很酷
长情又很酷 2020-12-18 16:15

I\'m writting a script in Google Aps (GAS) and I need to add a user(/writer) (administrator of the domain) to a spreadsheed of a another user.

The script is running/

相关标签:
2条回答
  • 2020-12-18 16:31

    After spending an hour of time, here is what I got This function will share the document to yourself (Domain administrator). You may change other parameters in xml to have read, write permission etc

    function shareDocumentToMe(){
      var base = 'https://docs.google.com/feeds/';
      var fetchArgs = googleOAuth_('docs', base);
      var writerID = Session.getEffectiveUser().getEmail();
      fetchArgs.method = 'POST';
      var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"
          +"<category scheme='http://schemas.google.com/g/2005#kind' "
          +"term='http://schemas.google.com/acl/2007#accessRule'/>"
          +"<gAcl:role value='writer'/>"
          +"<gAcl:scope type='user' value='"+writerID+"'/>"
          +"</entry>";
      fetchArgs.payload = rawXml;
      fetchArgs.contentType = 'application/atom+xml';
      var url = base + 'user@yourdomain.com/private/full/'+yourDocumentID+'/acl?v=3&alt=json';
      var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
      Logger.log(content);
    }
    
    //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("anonymous");
      oAuthConfig.setConsumerSecret("anonymous");
      return {oAuthServiceName:name, oAuthUseToken:"always"};
    }
    

    I have posetd the same code on my Google Site. You can have a look here. https://sites.google.com/site/appsscripttutorial/urlfetch-and-oauth

    0 讨论(0)
  • 2020-12-18 16:41

    I would never have found this. Thank you very much.

    I modified the following (var url = ...):

    'user@yourdomain.com/private/full/'
    

    by
    user + '/private/full/'

    where

    user = Session.getActiveUser (). getUserLoginId ()
    

    and also in the googleOAuth_ I put my domain values​​:

    oAuthConfig.setConsumerKey ("mydomain.com");
    
    oAuthConfig.setConsumerSecret ("xXxXxXxXxXxXxXxXx");
    

    And it works perfectly!

    I think it's very interesting that administrator of a domain can act on behalf of a user (once we have given permission) with some of the documents of the organization but have been created by the user.

    Also user resources (docs, spreadsheet, ...) so that a program can access (running with administrator permissions) without requiring the attention (or you do not have enough expertise) of the user, such as copies of security, ...

    In my case, I make a program to evaluate a Google Docs form has been created by a user (a teacher) as does the gadget "Flubaroo" but in a graphical environment (GUI) programmed by Google Sites with Google Apps Script (GAS).

    Thanks again for your time.

    Sergi

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