使用Javascript将数据插入Google Fusion Table

时间:2016-01-12 04:48:16

标签: javascript api insert google-fusion-tables fusion

我正在尝试使用JavaScript将数据插入我的Google Fusion表格。

我已经设置了OAuth,获得了ClientID和APIKey。

然而,当我尝试插入数据时,我得到“假”作为输出并且没有插入数据。我使用了以下代码:https://gmaps-samples.googlecode.com/svn/trunk/fusiontables/javascript-api.html

这是我的代码:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>

    var clientId = 'xxxxxxxxxxxxxxx.apps.googleusercontent.com';
    var apiKey = 'yyyyyyyyyyyyyy';

      var scopes = 'https://www.googleapis.com/auth/fusiontables';
      var tableId;

      // Initialize the client, set onclick listeners.
      function initialize() {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(function() { auth(true); }, 1);
      }

      // Run OAuth 2.0 authorization.
      function auth(immediate) {
        gapi.auth.authorize({
          client_id: clientId,
          scope: scopes,
          immediate: immediate
        }, handleAuthResult);
      }

      // Handle the results of the OAuth 2.0 flow.
      function handleAuthResult(authResult) {
        if (authResult) {
          return true;
        } else {
          return true;
        }
      }

  function setData(){
        var name = 'dummy name';
        var caseid = 'dummy case id';
        var tableID = "zzzzzzzzzzzzzzzzzz";
        var insert = [];
        insert.push('INSERT INTO ');
        insert.push(tableID);
        insert.push(' (Username, Decision) VALUES (');
        insert.push("'" + name + "', ");
        insert.push("'" + caseid+ "'");
        insert.push(')');
        query(insert.join(''));

    function query(query) {
        var lowerCaseQuery = query.toLowerCase();
        var path = '//www.googleapis.com/fusiontables/v2/query?'; 
        var callback = function(element) {
          return function(resp) {
            var output = JSON.stringify(resp);
            alert(output);  <<< I get false here as output and no data gets inserted
          };
        }
        if (lowerCaseQuery.indexOf('select') != 0 &&
            lowerCaseQuery.indexOf('show') != 0 &&
            lowerCaseQuery.indexOf('describe') != 0) {

          var body = 'sql=' + encodeURIComponent(query)
          runClientRequest({
            path: path,
            body: body,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': body.length
            },
            method: 'POST'
          }, callback('insert-data-output'));

        } else {
          runClientRequest({
            path: path,
            params: { 'sql': query }
          }, callback('select-data-output'));
        }
      }

      // Execute the client request.
      function runClientRequest(request, callback) {
        var restRequest = gapi.client.request(request);
        restRequest.execute(callback);
      }
  }
</script>
<script src="https://apis.google.com/js/client.js?onload=initialize"></script>
</head>

我已尝试为path

使用以下字符串
//www.googleapis.com/fusiontables/v2/query?

//developers.google.com/apis-explorer/#p/fusiontables/v1/fusiontables.query.sql?

/fusiontables/v2/query

/fusiontables/v1/query

0 个答案:

没有答案