Add the author to the spreadsheet through the Google Docs API List,

I am writing a script in Google Aps (GAS) and I need to add a user (/ writer) (domain administrator) to another user.

The script is started / called under administrator privileges.

First, I use β€œGoogle Apps administrative access to impersonate a domain user,” and I get a list of his documents (spreadsheets), and I get a document ID.

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

This part works fine and gets the desired identifier.

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

I am trying to do 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 document identifier.

This does not work,

Any ideas?

Sergi

+1
source share
2 answers

After spending an hour, this is what I got. This function will provide you with a document (domain administrator). You can change other parameters in xml to have read, write, 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 the same code on my google site. You can look here. https://sites.google.com/site/appsscripttutorial/urlfetch-and-oauth

+1
source

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

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

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

by
user + '/private/full/'

Where

 user = Session.getActiveUser (). getUserLoginId () 

and also in googleOAuth_ I put my domain values:

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

And it works great!

I am very interested that the domain administrator can act on behalf of the user (as soon as we gave permission) with some documents of the organization, but was created by the user.

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

In my case, I create a program for evaluating the form of Google Docs, which was created by the user (teacher), like the Flubaroo gadget, but in a graphical environment (GUI) programmed by Google Sites with Google Apps Script (GAS).

Thanks again for your time.

Sergi

0
source

All Articles