FusionTables私人表与OAUTH2

Al *_* Po 18 javascript oauth-2.0 google-fusion-tables

蒂姆罗森伯格的好照片,显示了OAUTH2的工作方式:

在此输入图像描述

我甚至懒得开始查看这2个 文件并进行测试, 所以我搜索了最简单的方法

1.get令牌

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>
Run Code Online (Sandbox Code Playgroud)

好,

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

正如你看到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);
});
Run Code Online (Sandbox Code Playgroud)

第二次警报工作正常,当你尝试Auth.再次,如果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>
Run Code Online (Sandbox Code Playgroud)

完善!

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

谢谢你给我理由回答自己!

Al *_* Po 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>
Run Code Online (Sandbox Code Playgroud)