2013-07-25 118 views
0

我試圖從我擁有的文檔中提取修訂歷史記錄。當我調試它時,我收到一個需要登錄的401錯誤。然而,我試圖使用匿名的ConsumerKey/ConsumerSecret來傳遞登錄信息,當我調試它時,urlFetch返回爲undefined。這是我到目前爲止的代碼:Google Apps腳本,需要使用匿名登錄的OAuth

//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); 
    } 

任何幫助將不勝感激。

回答