使用getLoginStatus触发Js重定向

时间:2011-10-02 06:54:02

标签: javascript authorization facebook-javascript-sdk oauth-2.0

我正在尝试使用Fb.getLoginStatus来实现Js重定向到另一个页面,如果用户登录到我的网站并登录到我的Fb应用程序后登录到Facebook。它不起作用,我不知道为什么!这可能吗?感谢任何建议。

这是我的代码 - 我使用PHP include将其插入到我的页面中,看起来Fb Javascript SDK init函数运行正常:

<script type="text/javascript">
  window.fbAsyncInit = function() {
    FB.init({
      appId  : 'my-app-id',
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true,  // parse XFBML
      channelUrl  : 'http://www.jamesherlihy.com/sandbox/fb-channel.html', // Custom Channel URL
      oauth : true //enables OAuth 2.0
    });
  };

  (function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
  }());

  FB.getLoginStatus(function(response) {

    /* if user is logged in to Fb and connected to Soulmap, redirect them to logged-in homepage */

    if (response.authResponse){
    window.location = "http://www.jamesherlihy.com/fb-03-logged-in.php";
    }

    /* if user is NOT logged in to Fb and/or connected to Soulmap, do not execute any script */

});
</script>

1 个答案:

答案 0 :(得分:0)

您需要确保页面中的某处有<div id="fb-root"></div>,因为脚本会引用它。

相关问题