FusionTables私人表与OAUTH2 [英] FusionTables private table with OAUTH2

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

问题描述



我是我甚至懒得开始看这个 2 文件测试
所以我搜索了最简单的方式



1.get令牌



2.使用该令牌进行访问



借助 gwt-oau th2



将它放入index.php头:
< script type =text / javascriptsrc = 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 =使用Google验证;
button.onclick = function(){

var req = {
'authUrl':GOOGLE_AUTH_URL,
'clientId':GOOGLE_CLIENT_ID,
'范围':['https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/fusiontables'
],
};

oauth2.login(req,function(token){
alert('有一个OAuth令牌:\ n'+令牌+'\ n'+'令牌过期'+ oauth2.expiresIn(req)+'ms\ n');
},函数(错误){
alert(错误:\ n+错误);
});
};

var dv = document.getElementById('admin-content');
dv.appendChild(button);
var clearTokens = document.createElement('button');
clearTokens.innerText ='清除所有代币'
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('有一个OAuth令牌:\ n'+ token +'\ n'
+ '令牌在'+ oauth2.expiresIn(req)+'ms\ n')中过期;
},函数(错误){
alert(错误:\ n+错误);
});

第二次警报正常,当您尝试验证时。再次,如果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 =您的浏览器似乎正在阻止此窗口与主窗口通信。;
}
< / script>
< / head>
< body>< / body>
< / html>

完美!



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



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

解决方案

把它放入 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 =您的浏览器似乎正在阻止此窗口与主窗口通信。;
}
< / script>
< / head>
< body>< / body>
< / html>


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:\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>

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

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

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