I can download the meta data of a text file on Google Drive, but I am unable to access the webContentLink through XMLHttpRequest (XMLHttpRequest.status = 0). A window.open(url) call with the same webContentLink url works fine though. Seems that CORS is not enabled for the webContentLink.
var clientId = '00000000000000';
var apiKey = 'AAAAAAAAAAAAAAAAAA';
var scopes = 'https://www.googleapis.com/auth/drive';
function loadDoc(url) {
//window.open(url);
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
alert("readyState = " + xmlhttp.readyState + " status = " + xmlhttp.status);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//... do something
}
}
xmlhttp.open("GET", url, true);
var myToken = gapi.auth.getToken();
xmlhttp.setRequestHeader("Referer", "http://mydomain");
xmlhttp.setRequestHeader("Accept", "text/x-tex");
xmlhttp.setRequestHeader("Content-Type", "text/x-tex");
xmlhttp.responseType = 'arraybuffer';
xmlhttp.overrideMimeType("text/plain");
xmlhttp.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token);
xmlhttp.send();
}
function loadMetaData(url) {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var metaData = xmlhttp.responseText;
var index = metaData.search('"webContentLink"');
if (index != -1) {
var i1 = metaData.indexOf('"', index + 17);
var i2 = metaData.indexOf('"', i1 + 1);
var fileName = metaData.slice(i1 + 1, i2);
loadDoc(fileName);
}
}
}
xmlhttp.open("GET", url, true);
var myToken = gapi.auth.getToken();
xmlhttp.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token);
xmlhttp.send();
}
// A simple callback implementation.
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
var url = 'https://www.googleapis.com/drive/v2/files/' + fileId;
loadMetaData(url);
}
}
So, function loadMetaData(url) works fine, and function loadDoc(url) does not. Am I right that this is due to CORS not set for the webContentLink, and is there any chance that this will be changed in the future?
Thanks, Danny