Search code examples
google-docs-apigoogle-apps-scriptgoogle-sheets-api

DocumentList API and GAS. How to marry them?


How about using this API?

developers.google.com/google-apps/documents-list

It seems to be able to access the revision history feed.

But the examples are in .NET. How do I apply/use this API in Google Apps Script? Anyone knows how and can shed some light? Maybe a short sample code?

Thanks.


Solution

  • You need to look on the protocol for DocLists API. You can use this protocol alongwith URLFetch and Google oAuth Here is a quick example, which returns the revision history in json format

    //Get revison history
    //resource_id is the id of the doc
    function getRevisionHistory(resource_id){
      var scope = 'https://docs.google.com/feeds/';
      var fetchArgs = googleOAuth_('docs', scope);
      fetchArgs.method = 'GET';
      var url = scope + 'default/private/full/'+resource_id+'/revisions?v=3&alt=json';
      var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
      var jsonFeed = Utilities.jsonParse(urlFetch.getContentText()).feed.entry;
      return jsonFeed
    }
    
    //Google oAuth
    //Used by getRevisionHistory
    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"};
    }
    

    To play more with DocsList API, you can see some more example on my Google Site https://sites.google.com/site/appsscripttutorial/urlfetch-and-oauth