0

I want to insert, update data from the fusion tables. While selecting from the fusion table all seems to work fine. But during row addition i need to used OAuth 2.0 but unable to find a suitable solution to get the access token and use it during the insert.

A code sample would help a lot.

  var fusiondata;
  function initialize() {

    // Initialize JSONP request
    var script = document.createElement('script');
    var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
    url.push('sql=');
    var query = 'insert into 1bPbx7PVJU9NaxgAGKqN2da4g5EbXDybE_UVvlAE (name,luckynumber) values('abc',89)'; 
    var encodedQuery = encodeURIComponent(query);
    url.push(encodedQuery);
    url.push('&callback=viewData');
    url.push('&key=AIzaSyA0FVy-lEr_MPGk1p_lHSrxGZDcxy6wH4o');
    script.src = url.join('');
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(script);
  }

  function viewData(data) {
  // code not required
  }
4

1 回答 1

1

我知道你们中的大多数人都在为 google auth 以及插入和更新融合表而受苦。我正在提供整个代码如何使用 gauth lib 以简单的方式插入

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Authorization Request</title>
    <script src="https://apis.google.com/js/client.js"></script>
        <script type="text/javascript">
          function auth() {
            var config = {
              'client_id': '365219651081-7onk7h52kas6cs5m17t1api72ur5tcrh.apps.googleusercontent.com',
              'scope': 'https://www.googleapis.com/auth/fusiontables'
            };
            gapi.auth.authorize(config, function() {
              console.log('login complete');
              console.log(gapi.auth.getToken());
            });
          }
          function insert_row(){
                alert("insert called");
                gapi.client.setApiKey('AIzaSyA0FVy-lEr_MPGk1p_lHSrxGZDcxy6wH4o');

                var query = "INSERT INTO 1T_qE-o-EtX24VZASFDn6p3mMoPcWQ_GyErJpPIc(Name, Age) VALUES ('Trial', 100)";

                gapi.client.load('fusiontables', 'v1', function(){
                    gapi.client.fusiontables.query.sql({sql:query}).execute(function(response){console.log(response);});
                });

            }
        </script>
    </head>

    <body>
    <button onclick="auth();">Authorize</button>
    <p>&nbsp;</p>
    <button onclick="insert_row();">Insert Data</button>
    </body>
    </html>
于 2012-12-03T16:55:34.217 回答