Search code examples
javapostgoogle-fusion-tablesgoogle-api-java-clientgoogle-oauth

sending SQL INSERT query as a parameter in POST request in FusionTable API


I am accessing the fusiontable API using service account and want to send a POST request to insert values in a fusiontable. I am getting the following error:

    400 Bad Request
     {
   "error": {
  "errors": [
  {
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
  "code": 400,
 "message": "Parse Error"
 }
 }

This is my code, Please let me know where i am going wrong.

            String PostUrl = "https://www.googleapis.com/fusiontables/v1/tables";
            HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);

            //generate the REST based URL
            GenericUrl url = new GenericUrl(PostUrl.replaceAll(" ", "%20"));
            //make POST request
            String requestBody = "sql=INSERT INTO 1DqxKZUct_VhfG3fZy6Lru1CamniDO8vv5qW6jO8 (Species) VALUES ('Red Shoes')";
            HttpRequest request = requestFactory.buildPostRequest(url, ByteArrayContent.fromString("application/json", requestBody));
            request.getHeaders().setContentType("application/json");
            request.getHeaders().set("sql", "INSERT INTO 1DqxKZUct_VhfG3fZy6Lru1CamniDO8vv5qW6jO8 (Species) VALUES ('Red Shoes')");
            System.out.println("HttpRequest request" + request);
            HttpResponse response = request.execute();
            String content = response.parseAsString();

Solution

  • You need to send the request to query end point of the Google API service and not the tables end point. i.e change the PostUrl value as

            String PostUrl = "https://www.googleapis.com/fusiontables/v1/query";