2012-11-29 37 views
0

我已經擁有令牌並且可以訪問我的令牌,其範圍是可融合的。 http://www.udayan2k12.com/token.html使用oauth令牌更新Google fusiontable?

<script type="text/javascript"> 
    (function() { 
    var GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth"; 
    var GOOGLE_CLIENT_ID = "365219651081-7onk7h52kas6cs5m17t1api72ur5tcrh.apps.googleusercontent.com"; 
    var PLUS_ME_SCOPE = "https://www.googleapis.com/auth/fusiontables"; 

    var button = document.createElement("button"); 
    button.innerText = "Authenticate with Google"; 
    button.onclick = function() { 
     var req = { 
     "authUrl" : GOOGLE_AUTH_URL, 
     "clientId" : GOOGLE_CLIENT_ID, 
     "scopes" : [ PLUS_ME_SCOPE ], 
     }; 
     oauth2.login(req, function(token) { 
     alert("Got an OAuth token:\n" + token + "\n" 
      + "Token expires in " + oauth2.expiresIn(req) + " ms\n"); 
      document.getElementById('token').innerHTML = token; 
     }, function(error) { 
     alert("Error:\n" + error); 
     }); 
    }; 
    document.body.appendChild(button); 

    var clearTokens = document.createElement("button"); 
    clearTokens.innerText = "Clear all tokens"; 
    clearTokens.onclick = oauth2.clearAllTokens; 
    document.body.appendChild(clearTokens); 
    })(); 
    </script> 

但問題是,我無法使用該令牌來更新融合表。 我想通過使用JavaScript專門更新它。

能有人給我提供的代碼中使用此令牌使用融合表的SQL

回答

0

我知道你們大多數人都爲谷歌驗證和插入和更新融合表而苦惱。 我提供整個代碼如何使用gauth lib以簡單的方式插入

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<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> 
2

您應該使用JavaScript Google API client更新fusiontable,那麼它很容易使身份驗證請求,谷歌的API(見their "Samples" page) :

gapi.client.setApiKey('YOUR API KEY'); 
var query = "INSERT INTO tableId (id, name) VALUES (1, 'test')"; 
gapi.client.load('fusiontables', 'v1', function(){ 
      gapi.client.fusiontables.query.sql({sql:query}).execute(function(response){console.log(response);}); 
     });