1

我一直在使用以下 JavaScript 通过 Facebook 登录我的用户。它已经工作了几个星期,但突然停止工作了。

当我单击登录按钮时,常规的 Facebook 连接窗口正在打开,但是当我登录时,会话似乎仍未设置。

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: 'MY_APP_ID',cookie: true,xfbml: true,channelUrl: 'http://MYURL.com/channel.php',oauth: true});};
(function() {var e = document.createElement('script');
e.async = true;e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);}());

function CallAfterLogin(){
        FB.login(function(response) {       
        if (response.status === "connected") 
        {
            LodingAnimate(); //Animate login
            FB.api('/me', function(data) {

                window.location = 'http://MYURL.com/<?=$_SESSION['lang']; ?>';

          });
         }
    });
}

</script>

channel.php中的代码:

<?php
$cache_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: max-age=".$cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
?><script src="//connect.facebook.net/en_US/all.js"></script>
4

1 回答 1

2

这是您应该如何处理用户状态:

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
            FB.init({
              appId      : 'MY_APP_ID', // App ID
              channelUrl : 'http://MYURL.com/channel.php', // Channel File
              status     : true, // check login status
              cookie     : true, // enable cookies to allow the server to access the session
              xfbml      : true  // parse XFBML
            });

            // Additional initialization code here
            FB.getLoginStatus(function (response) {
            if (response.status === 'connected') {
               FB.api('/me', function(data) {

                window.location = 'http://MYURL.com/<?=$_SESSION['lang']; ?>';

               });
            } else if (response.status === 'not_authorized') {
                //you must ask permissions to the user
            } else {
                //user is not logged, just change the scope for the ones you want or remove
                //it if you don't need any additional permissions
                FB.login(function(response) { }, {scope:'email,user_likes'}); 
            }
        });
        };

        // Load the SDK Asynchronously
        (function(d){
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/pt_PT/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));
</script>

编辑

这是您页面的当前代码

<body>

<div id="fb-root" class=" fb_reset"><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div><iframe name="fb_xdm_frame_http" frameborder="0" allowtransparency="true" scrolling="no" id="fb_xdm_frame_http" aria-hidden="true" title="Facebook Cross Domain Communication Frame" tab-index="-1" style="border: none;" src="http://static.ak.facebook.com/connect/xd_arbiter.php?version=21#channel=f2c633f99&amp;origin=http%3A%2F%2Ftictam.com&amp;channel_path=%2Fse%3Ffb_xd_fragment%23xd_sig%3Dfd496acc4%26"></iframe><iframe name="fb_xdm_frame_https" frameborder="0" allowtransparency="true" scrolling="no" id="fb_xdm_frame_https" aria-hidden="true" title="Facebook Cross Domain Communication Frame" tab-index="-1" style="border: none;" src="https://s-static.ak.facebook.com/connect/xd_arbiter.php?version=21#channel=f2c633f99&amp;origin=http%3A%2F%2Ftictam.com&amp;channel_path=%2Fse%3Ffb_xd_fragment%23xd_sig%3Dfd496acc4%26"></iframe></div></div><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div><iframe name="f68921bbc" frameborder="0" allowtransparency="true" scrolling="no" style="display: none;" src="https://www.facebook.com/dialog/oauth?client_id=APP_ID&amp;response_type=token%2Csigned_request%2Ccode&amp;display=none&amp;domain=tictam.com&amp;origin=1&amp;redirect_uri=http%3A%2F%2Fstatic.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D21%23cb%3Df1f63150cc%26origin%3Dhttp%253A%252F%252Ftictam.com%252Ff2c633f99%26domain%3Dtictam.com%26relation%3Dparent&amp;sdk=joey"></iframe></div></div></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/sv_SE/all.js#xfbml=1&appId=APP_ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>




<div id="intro_form" style="margin-top: -2px; top: 0px; position: fixed; z-index: 100; min-height: 50px;">

    <a href="/se"><img src="/logo3.png" style="margin-bottom: -19px;" border="0"></a>

<form method="post" action="/se/find" style="margin: 0px; padding: 0px; clear: none; display: inline;" id="search_form">
   <input type="text" name="what" placeholder="Vad?" value=""> <input type="text" name="where" placeholder="Var?" value=""> <input type="submit" name="search" id="intro_submit" value="Sök!" style="width: 224px !important; cursor: pointer; margin-right: 4px;"> 
   </form>


   <form method="post" action="/se/find" style="margin: 0px; padding: 0px; clear: none; display: none; margin-top: 20px;" id="search_form_2">
   <input type="text" name="what" placeholder="Vad?" value=""> <input type="text" name="where" placeholder="Var?" value=""> <input type="submit" name="search" id="intro_submit" value="Sök!" style="width: 224px !important; cursor: pointer; margin-right: 4px;"> 
   </form>

   <img src="/search-icon.png" width="35" height="35" id="search_icon" style="display: none; margin-top: 11px; margin-right: 11px; float: right; clear: both;">


    </div>


    <script type="text/javascript">

    $(document).ready(function()
    {
        if ( $(window).width() < 900) 
        {
            $("#search_form").hide();
            $("#search_icon").show();
        }
    });

    $("#search_icon").click(function()
    {
        $("#search_form_2").toggle("slow");
    });

    </script>

    <div style="height: 50px;"></div>

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
            FB.init({
              appId      : '360183997416092', // App ID
              channelUrl : 'http://tictam.com/channel.php', // Channel File
              status     : true, // check login status
              cookie     : true, // enable cookies to allow the server to access the session
              xfbml      : true  // parse XFBML
            });

            // Additional initialization code here
            FB.getLoginStatus(function (response) {
            if (response.status === 'connected') {
               FB.api('/me', function(data) {

                window.location = 'http://tictam.com/se';

               });
            } else if (response.status === 'not_authorized') {
                //you must ask permissions to the user
            } else {
                //user is not logged, just change the scope for the ones you want or remove
                //it if you don't need any additional permissions
                FB.login(function(response) { }, {scope:'email, user_work_history, user_education_history'}); 
            }
        });
        };

        // Load the SDK Asynchronously
        (function(d){
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/pt_PT/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));
</script>

正如您在标签旁边看到的那样,<body>您正在加载一个 javascript sdk,然后在底部您再次使用我的代码加载,删除第一个并将我的代码移动到<body>标签旁边

于 2013-04-15T15:35:39.513 回答