谷歌驱动器:通过JavaScript访问webContentLink

时间:2012-08-08 12:36:15

标签: javascript google-drive-api

我可以在Google云端硬盘上下载文本文件的元数据,但我无法通过XMLHttpRequest(XMLHttpRequest.status = 0)访问webContentLink。使用相同webContentLink url的window.open(url)调用可以正常工作。似乎没有为webContentLink启用CORS。

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

因此,函数loadMetaData(url)工作正常,而函数loadDoc(url)则没有。我是对的,这是因为没有为webContentLink设置CORS,并且将来是否有可能改变它?

谢谢, 丹尼

2 个答案:

答案 0 :(得分:4)

如果通过XHR下载文件,则应使用downloadUrl代替,并将访问令牌作为Authorization标题提供。

webContentLink仅支持cookie身份验证,从XHR检索时可能会失败。

答案 1 :(得分:3)

编辑:谷歌api已经改变,现在需要一个oauthToken,我已经添加了。

解决!感谢Alain。您可以在下面找到正确的代码。可以找到一个工作示例here

var clientId = '1234567890';
var scopes = 'https://www.googleapis.com/auth/drive';
var oauthToken;

function getData(url, callback) {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open('GET', url, true);
    var myToken = gapi.auth.getToken();
    xmlhttp.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token);
    xmlhttp.send();
}

// Create and render a Picker object
function createPicker() {
    var picker = new google.picker.PickerBuilder()
        .setAppId(clientId)
        .setOAuthToken(oauthToken)
        .addView(google.picker.ViewId.DOCS)
        .setCallback(pickerCallback)
        .build();
    picker.setVisible(true);
}

// 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;
        getData(url, function(responseText) {
            var metaData = JSON.parse(responseText);
            getData(metaData.downloadUrl, function(text) {
                //Do something with text...
            });
        });
    }
}