Скрипт Google Apps, OAuth, требующий входа в систему с анонимным доступом

Я пытаюсь извлечь историю изменений из документа, которым владею. Когда я отлаживаю его, я получаю ошибку 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);
   }

Любая помощь будет принята с благодарностью.


person Billy Erb    schedule 25.07.2013    source источник


Ответы (1)


Попробуйте добавить ключ API (&key=mykey) к вызову UrlFetch в Drive API. Вы можете включить Drive API и получить ключ API на странице http://developer.google.com/console.

Подробнее читайте здесь: https://developers.google.com/console/help/#generatingdevkeys

person Arun Nagarajan    schedule 25.07.2013