2013-07-25 5 views
0

J'essaie d'extraire l'historique des révisions d'un document que je possède. Lorsque je le débogue, je reçois une erreur 401 nécessitant une connexion. Cependant, j'essaie d'utiliser le ConsumerKey/ConsumerSecret anonyme pour passer le login et quand je le débogue, urlFetch revient comme indéfini. Voici le code que j'ai jusqu'à présent:Script Google Apps, OAuth nécessitant une connexion avec Anonymous

//Get revison history 
//fileId is the ID of the resource from Google Docs 
function getRevisionHistory(fileId){ 

//Scope 
var scope = 'https://www.googleapis.com/auth/drive'; 

//Get Google oAuth Arguments 
var fetchArgs = googleOAuth_('docs', scope); 

//Set the fetch method 
fetchArgs.method = 'LIST'; 

//Feed URL 
var url = 'https://www.googleapis.com/drive/v2/files/' + fileId + '/revisionsv=3&alt=json'; 
var urlFetch = UrlFetchApp.fetch(url); 

//Get the json of revision history entry 
var jsonFeed = Utilities.jsonParse(urlFetch.getContentText()).feed.entry; 
//return the revison history feed 
return jsonFeed 
} 

//Google oAuth 
//Used by getRevisionHistory 
function googleOAuth_(name, scope) { 
    var oAuthConfig = UrlFetchApp.addOAuthService(name); 
    oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
    oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); 
    oAuthConfig.setConsumerKey("anonymous"); 
    oAuthConfig.setConsumerSecret("anonymous"); 
return {oAuthServiceName:name, oAuthUseToken:"always"}; 
} 

function trackChanges(jsonFeed){ 
    var doc = DocumentApp.getActiveDocument(); 
    var docName = doc.getName(); 
    var docId = doc.getId(); 

    getRevisionHistory(docId) 
    var jsonString = Utilities.jsonStringify(jsonFeed); 
    var changes = DocumentApp.create("Track Changes for " + docName); 
    var text = changes.getBody().editAsText(); 
    text.appendText(jsonString); 
    } 

Toute aide serait grandement appréciée.

Répondre

Questions connexes