0

我正在尝试从我拥有的文档中提取修订历史记录。当我调试它时,我收到一个需要登录的 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);
   }

任何帮助将不胜感激。

4

1 回答 1

0

尝试将 API 密钥 ( &key=mykey) 添加到对 Drive API 的 UrlFetch 调用。您可以在http://developer.google.com/console启用 Drive API 并获取 API 密钥。

在此处阅读更多帮助 - https://developers.google.com/console/help/#generatingdevkeys

于 2013-07-25T21:18:35.640 回答