Question

I would like to write a small Google Chrome extension in which I use gooles fusion tables as a central data base.

Using existing tables and data (added via the GooleDoc-Webside) works quite fine (SELECT, SHOW TABLES, DESCRIBE) but if I try to do any changes (for this I need a "POST" request). I always get the following error: "Missing sql parameter." I have no Idea why this happens. I tried already alot (e.g. "create table test3(A:number,B:number)", insert .)

The code I wrote is the following:

function Fusion(readyCallback) {
    var myObject = {
        URL : "https://www.google.com/fusiontables/api/query",
        authToken : null,
        doGET : function(command,callback) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("get"
                        ,this.URL+"?sql="+encodeURI(command)
                        ,true);
            xmlhttp.setRequestHeader("Authorization"
                                    ,"GoogleLogin auth=" + this.authToken);
            xmlhttp.onreadystatechange = function() { callback(xmlhttp.responseText); }
            xmlhttp.send();
        },
        doPOST : function(command,callback) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("post"
                        ,this.URL
                        ,true);
            xmlhttp.setRequestHeader("Authorization"
                                    ,"GoogleLogin auth=" + this.authToken);
            xmlhttp.onreadystatechange = function() { callback(xmlhttp.responseText); }
            xmlhttp.send("sql="+encodeURI(command));
        }
    }
    // client login authentification
    var email    = "XXX@gmail.com";
    var password = "XXX";
    var loginURL = "https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email="
              + encodeURI(email) + "&Passwd=" + encodeURIComponent(password) 
              + "&service=fusiontables&Source=testing";
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("get",loginURL,true);
    xmlhttp.onreadystatechange = function() {
        var tmp = xmlhttp.responseText;
        tmp = tmp.slice(tmp.search("Auth=") + 5, tmp.length);
        tmp = tmp.replace(/\n/g, "");
        tmp = tmp.replace(/\r/g, "");
        myObject.authToken = tmp;
        console.log("authentifiaction token status: "+xmlhttp.statusText);
        if (readyCallback) {
            readyCallback();
        }                                  
    };
    xmlhttp.send();

    return myObject;
}

If I for example try to do something like this

mf.doPOST("INSERT INTO 2664928(ID,Text,Number) VALUES (2,'Hallo',3)",mc)

or this

mf.doPOST("INSERT INTO 2664928(col4,col0,col1) VALUES (2,'Hallo',3)",mc)

with a table, described by this

column id,name,type
col4,ID,number
col0,Text,string
col1,Number,number

I keep getting this error (the same when I try to create a table)

Any suggestions? I am realy confused!

Was it helpful?

Solution

The message about the "Missing sql parameter." seems not to be the real error.

When I correctly understood the issue, it is currently not possible to POST data to Fusion Tables using JavaScript. If GFT would send to appropriate CORS headers for POST requests, it could actually work. But this is not the case as of now.

See issue 554 (http://code.google.com/p/fusion-tables/issues/detail?id=554).

OTHER TIPS

Try adding a content-length header to the request:

var query = "sql="+encodeURI(command);
xmlhttp.setRequestHeader("Content-length", query.length);

Also, I would recommend sending the ClientLogin request as a POST request rather than the GET request, so that the username and password are not sent as part of the URL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top