Search code examples
c#javascriptasp.netgoogle-fusion-tablesinsert-update

OAuth 2.0 access using javascript


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
  }

Solution

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