This has come up before, related to the Google Drive SDK: How do I get exportLinks for revisions in Google Drive API.
My problem isn't in getting the exportLinks - just that the ones provided by the API don't work.
Here's a modified version of the "Listing revisions" example from the Advanced Drive Service documentation, that logs the exportLinks
for each revision of a given fileId
.
function listRevisions(fileId) {
var revisions = Drive.Revisions.list(fileId);
if (revisions.items && revisions.items.length > 0) {
for (var i = 0; i < revisions.items.length; i++) {
var revision = revisions.items[i];
var date = new Date(revision.modifiedDate);
Logger.log('Date: %s, PDF exportLink: %s',
date.toLocaleString(),
revision.exportLinks[MimeType.PDF] );
}
} else {
Logger.log('No revisions found.');
}
}
Logs
Here are sample logs for a test document that has two "major" revisions. The revision numbers are provided explicitly in the exportLinks.
[14-11-13 16:40:50:511 EST] Date: November 13, 2014 4:35:55 PM EST,
PDF exportLink: https://docs.google.com/feeds/download/documents/export/Export?id=1V2zkXfyRGh_6gnCXtWlII6sxMQEDcLApRrEk-giIE2s&revision=28&exportFormat=pdf
[14-11-13 16:40:50:512 EST] Date: November 13, 2014 4:37:51 PM EST,
PDF exportLink: https://docs.google.com/feeds/download/documents/export/Export?id=1V2zkXfyRGh_6gnCXtWlII6sxMQEDcLApRrEk-giIE2s&revision=32&exportFormat=pdf
So far, so good. Except that those links open the SAME version of the document... the latest. (Go ahead, try them - the document is public.)
Question: Is there some format of exportLinks that will actually download the specified revisions? (i.e. maybe the 'revision' parameter should be named something else)
Google engineers have reproduced the problem using the code shown below, and have raised an internal bug report regarding this. While the sample code is in Google Apps Script, the problem is in Google Drive itself.
You may track any progress by visiting and starring Issue 4008 on the google-drive-sdk Issue Tracker.
function test() {
var content = Utilities.newBlob('Apple', 'text/plain');
var file = {
title: 'Test Document'
};
file = Drive.Files.insert(file, content, {
convert: true
});
var doc = DocumentApp.openById(file.id);
doc.getBody().appendParagraph('Banana');
doc.saveAndClose();
var oauthToken = ScriptApp.getOAuthToken();
var revisions = Drive.Revisions.list(file.id).items;
revisions.forEach(function(revision) {
// revision = revisions[];
Object.keys(revision.exportLinks).forEach(function(mimeType) {
var link = revision.exportLinks[mimeType];
var response = UrlFetchApp.fetch(link, {
headers: {
Authorization: 'Bearer ' + oauthToken
}
});
var blob = response.getBlob();
var parts = blob.getName().split('.');
blob.setName(parts[0] + '-' + revision.id + '.' + parts[1]);
DriveApp.createFile(blob);
});
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With