谷歌应用程序脚本,OAuth要求匿名登录
我正试图从我拥有的文档中提取修订历史记录。当我调试它时,我收到一个401错误,需要登录。但是,我尝试使用匿名ConsumerKey/ConsumerCret传递登录名,当我调试它时,urlFetch返回为未定义。以下是我目前掌握的代码:谷歌应用程序脚本,OAuth要求匿名登录,oauth,google-apps-script,revision-history,Oauth,Google Apps Script,Revision History,我正试图从我拥有的文档中提取修订历史记录。当我调试它时,我收到一个401错误,需要登录。但是,我尝试使用匿名ConsumerKey/ConsumerCret传递登录名,当我调试它时,urlFetch返回为未定义。以下是我目前掌握的代码: //Get revison history //fileId is the ID of the resource from Google Docs function getRevisionHistory(fileId){ //Scope var scope =
//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);
}
任何帮助都将不胜感激。尝试向驱动器API的UrlFetch调用添加API键(&Key=mykey
)。您可以在启用驱动器API并获取API密钥
请在此处阅读更多帮助-