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
  }
slfan
  • 8,950
  • 115
  • 65
  • 78
  • 1
    Are you looking for a JavaScript only solution? I guess that you want to update a specific table that is under your control. This use case requires OAuth, a so called [Service Account](https://developers.google.com/accounts/docs/OAuth2ServiceAccount) and a little bit of server-side code to handle the authentication. Once you have the access token, it's simple. See [my answer to a similar question](http://stackoverflow.com/questions/11595122/google-fusion-table-api-v1-properly-composed-rest-format-to-post-an-update/11619373#11619373) for more information. – Odi Sep 03 '12 at 20:03
  • What does not work? Do you see any errors in the JavaScript console? Please note that the example only works to get access to a users Fusion Table, but maybe that's what you want. – Odi Sep 04 '12 at 06:11
  • Did you replace the API key and the client id in the example with your own? If not, please try that first. – Odi Sep 04 '12 at 15:40
  • please check [http://www.udayan2k12.com/trial.html ](http://www.udayan2k12.com/trial.html) Tell me whats wrong. My ClientID Details Client ID: 365219651081-istfrdgsvrtj324sufau0ldi5e8b3fmk.apps.googleusercontent.com Email address: 365219651081-istfrdgsvrtj324sufau0ldi5e8b3fmk@developer.gserviceaccount.com Redirect URIs: none JavaScript origins: https://www.udyan2k12.com – user1637982 Sep 05 '12 at 12:26
  • Check the details [Here](http://stackoverflow.com/questions/12281668/insert-into-fusion-table-using-javascript-and-oauth2) – user1637982 Sep 05 '12 at 12:42

1 Answers1

1

I know most of you are suffering for google auth and inserting and updating fusion table. I am providing the entire code how to use the gauth lib to insert in a simple manner

    <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>