带有 OAUTH2 的 FusionTables 私有表 [英] FusionTables private table with OAUTH2

查看:30
本文介绍了带有 OAUTH2 的 FusionTables 私有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点懒惰,甚至开始查看这个2 文件测试所以我寻找最简单的方法

1.获取令牌

2.使用该令牌访问

的帮助下gwt-oauth2

放入 index.php head 中:<script type="text/javascript" src="gwt-oauth2.js"></script>

这个在身体里

好的,

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

如您所见,access_token 运行良好,但

你仍然没有得到的是第一个警报:

oauth2.login(req, function(token) {alert('得到了一个 OAuth 令牌:
' + 令牌 + '
'+ '令牌在 ' + oauth2.expiresIn(req) + ' ms
');}, 函数(错误){alert("错误:
" + 错误);});

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

<头><script type="text/javascript">如果(window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin){window.opener.oauth2.__doLogin(location.hash);} 别的 {document.body.innerText = "您的浏览器似乎阻止了此窗口与主窗口的通信.";}<body></body>

完美!

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

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

解决方案

放入oauthWindow.html文件

<头><script type="text/javascript">如果(window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin){window.opener.oauth2.__doLogin(location.hash);} 别的 {document.body.innerText = "您的浏览器似乎阻止了此窗口与主窗口的通信.";}<body></body>

Good pic by Tim Rosenberg that shows exactly how OAUTH2 work's:

I'm kind a lazy to even start looking on this 2 files and test so I searched for easyest way to

1.get token

2.access with that token

with help of gwt-oauth2

put it into index.php head : <script type="text/javascript" src="gwt-oauth2.js"></script>

and this in body

<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:
'+ token +'
'+ 'Token expires in '+ oauth2.expiresIn(req) +' ms
');
  }, function(error) {
    alert("Error:
" + 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>

OK,

Now you can see connection and redirection to oauthWindow.html in new window without errors. GET parameters now showing you access_token token_type expires_in. Check the access_token HERE

As you see access_token working great BUT

What you still don't get is first alert from that :

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

Second alert works fine and when you try to Auth. again if oauthWindow.html still open it shows you an error alert(so it's working!) Now let's add that little code to 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>

Perfect!

Now if you want to work with private tables all you need is to add an access_token to url.

Thanks for giving me the reason to answer myself!

解决方案

Put this into oauthWindow.html file

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

这篇关于带有 OAUTH2 的 FusionTables 私有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆