When it downloads files from exportLinks using UrlFetchApp.fetch()
, the authorization is required. So please modify your script as follows.
From :
var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"];
var revString = UrlFetchApp.fetch(revUrl, { contentType: "text/plain", }).getContentText();
To :
var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"] + "&access_token=" + ScriptApp.getOAuthToken();
var revString = UrlFetchApp.fetch(revUrl).getContentText();
By this, you can download text data from the revision data.
Edit :
function revisionHistoryLite() {
var doc = DocumentApp.getActiveDocument();
var eds = doc.getEditors();
var body = doc.getBody();
var revs = Drive.Revisions.list(doc.getId())
var editsList = [];
for(var i=0; i<revs.items.length; i++) {
var revision = revs.items[i];
editsList.push([revision.id, revision.kind, revision.modifiedDate, revision.lastModifyingUser.emailAddress]);
if(revision.lastModifyingUser.emailAddress == "### mail address ###") {
var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"] + "&access_token=" + ScriptApp.getOAuthToken();
var revString = UrlFetchApp.fetch(revUrl).getContentText();
Logger.log(revString); // Contains full HTTP markup
}
}
}
Updated: February 7, 2020
From January, 2020, the access token cannot be used with the query parameter like access_token=###
. Ref So please use the access token to the request header instead of the query parameter. It's as follows.
var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…