Search code examples
google-docs-apigoogle-apps-script

Add an writer to a spreadsheet via "Google Document List API"


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/invoked under administrator privilegies.

First I use "Google Apps administrative access to impersonate a user of the domain" and I get a list of his documents (spreadsheets) and I get the Id of the documents.

var oauthConfig = UrlFetchApp.addOAuthService("docs"); oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken? (...)

This part is working fine and get the desired Id's.

Now, I want to add to myself (administrator@example.com) to this document

I'm trying this:

var optAdvancedArgs = {
"contentType": "application/atom+xml",
"method": "POST",
"headers": {  "GData-Version": "3.0" }, 
              "oAuthServiceName": "docs", 
              "oAuthUseToken": "always",
           };

var url_set = "https://docs.google.com/feeds/" + user + "/private/full/document:" + matriu2[0][6] + '/acl/user:administrator@example.com'

var result = UrlFetchApp.fetch(url_set, optAdvancedArgs);

"user" is the owner of the document, the active user (Session.getActiveUser().getUserLoginId()) and "matriu2[nm][6]" is the Id of the document.

This don't work,

Any ideas ?

Sergi


Solution

  • 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