我正在尝试从我拥有的文档中提取修订历史记录。当我调试它时,我收到一个需要登录的 401 错误。但是,我正在尝试使用匿名的 ConsumerKey/ConsumerSecret 来传递登录信息,当我调试它时,urlFetch 会以未定义的形式返回。这是我到目前为止的代码:
//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);
}
任何帮助将不胜感激。