FusionTables私人表与OAUTH2

时间:2012-06-12 12:17:03

标签: javascript oauth-2.0 google-fusion-tables

Tim Rosenberg的好照片,显示了OAUTH2的确切工作方式:

enter image description here

我甚至懒得开始关注2 filestest 所以我搜索了最简单的方式

1.get token

2.使用该令牌进行访问

gwt-oauth2

的帮助下

把它放到index.php头: <script type="text/javascript" src="gwt-oauth2.js"></script>

并在体内

<script type="text/javascript">
(function() {
var GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth";
var GOOGLE_CLIENT_ID = "CLIENT_ID";
//var PLUS_ME_SCOPE = "https://www.googleapis.com/auth/plus.me";
//var FusionTable_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': ['https://www.googleapis.com/auth/plus.me',
               'https://www.googleapis.com/auth/fusiontables'
              ],
};

oauth2.login(req, function(token) {
    alert('Got an OAuth token:\n'+ token +'\n'+ 'Token expires in '+ oauth2.expiresIn(req) +' ms\n');
  }, function(error) {
    alert("Error:\n" + error);
  });
};

var dv = document.getElementById('admin-content');
dv.appendChild(button);
var clearTokens = document.createElement('button');
clearTokens.innerText = 'Clear all tokens'
clearTokens.onclick = oauth2.clearAllTokens;
dv.appendChild(clearTokens);
})();
</script>

好的,

现在,您可以在新窗口中看到连接和重定向到oauthWindow.html而不会出现错误。 GET参数现在向您显示access_token token_type expires_in。检查access_token HERE

正如您所见,access_token工作得很好但是

你仍然没有得到的是第一个提醒:

oauth2.login(req, function(token) {
  alert('Got an OAuth token:\n' + token + '\n'
  + 'Token expires in ' + oauth2.expiresIn(req) + ' ms\n');
}, function(error) {
  alert("Error:\n" + error);
});

第二次警报正常,当您尝试验证时。再次,如果oauthWindow.html仍然打开它会显示一个错误警报(所以它正在工作!) 现在让我们将这个小代码添加到oauthWindow.html

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
      if (window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin) {
        window.opener.oauth2.__doLogin(location.hash);
      } else {
        document.body.innerText = "Your browser seems to be stopping this window from communicating with the main window.";
      }
    </script>
  </head>
  <body></body>
</html>

完美!

现在,如果你想使用私有表,你只需要将一个access_token添加到url。

感谢您给我自己回答的理由!

1 个答案:

答案 0 :(得分:1)

将其放入oauthWindow.html文件

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
      if (window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin) {
        window.opener.oauth2.__doLogin(location.hash);
      } else {
        document.body.innerText = "Your browser seems to be stopping this window from communicating with the main window.";
      }
    </script>
  </head>
  <body></body>
</html>