简体   繁体   中英

Google Apps Script, OAuth requiring Login with Anonymous

I'm trying to pull the revision history from a document that I own. When I debug it, I receive a 401 Error requiring login. However, I'm trying to use the anonymous ConsumerKey/ConsumerSecret to pass the login and when I debug it, urlFetch comes back as undefined. Here's the code I have so far:

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

Any help would be greatly appreciated.

Try adding an API Key ( &key=mykey ) to the UrlFetch call to the Drive API. You can enable Drive API and get an API key at http://developer.google.com/console .

Read more for help here - https://developers.google.com/console/help/#generatingdevkeys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM