Facebook会话在我的应用程序中经常被破坏


facebook session gets destroyed frequently in my application?

我使用facebook sdk登录我的应用程序后,我得到了facebook用户ID,但一段时间后它会自动被销毁。这种情况经常发生。我需要时不时地刷新页面.谁能帮我找到为什么会发生这种情况的解决方案?我同时使用javascript和php sdk。我已经搜索了相同的内容,但我没有任何适当的解决方案。

请有人帮我解决这个问题...

这是我用来登录的代码:

<div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                    appId: '<?php echo $facebook->getAppID() ?>',
                    cookie: true,
                    xfbml: true,
                    oauth: true
                });
                FB.Event.subscribe('auth.login', function(response) {
                    window.location.reload();
                });
                FB.Event.subscribe('auth.logout', function(response) {
                    window.location.reload();
                });
            };
            (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 loginfb()
            {
                FB.login(function(response) {
                    if (response.authResponse) {
                        //alert('Success!');
                    } else {
                        //alert('Login Failed!');
                    }
                }, {scope: 'manage_pages,publish_actions,email'});
            }
            (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/en_US/all.js#xfbml=1&appId=<?php echo APP_ID_KEY; ?>";
                fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'));

这是我用来获取Facebook用户ID的PHP代码:

require_once('facebook.php');

$facebook = new Facebook(array(
  'appId'  => APP_ID_KEY,
  'secret' => APP_SECRET,
));
$site_url = CALLBACK_URL;
// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
        $friends = $facebook->api('/me/friends');
    $token=$facebook->getAccessToken();
  } catch (FacebookApiException $e) {
    error_log($e);`enter code her`enter code here`e`
    $user = null;
  }
}

现在,当我第一次登录时,我正在获取Facebook用户ID,但一段时间后它会自动变为零,因此我需要再次单击登录按钮以获取用户ID,这种情况经常发生,而且非常烦人。

    会话
  1. 垃圾回收器可以删除会话文件(如果未修改)
  2. 可能您正在使用短寿命访问令牌,因此当您调用FB api时,您会收到清除FB会话的异常
  3. 我在现场使用 JS 和 PHP SDK 时遇到了问题,PHP 正在解释 JS cookie(共享会话 == false,在 JS 身份验证中您收到短寿命访问令牌),我通过在 JS init 中消除 cookie 来解决它。

PS检查Facebook例外在说什么

这是

JS端

window.fbAsyncInit = function() {
    FB.init({
    appId      : app_id, 
    status     : true,
    });         
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/pl_PL/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));

//callback - function called after succeed login 
//opts - The same opts as in FB.login().
function login(callback, opts)
{
    //opts must be an object
    if (!opts || typeof(opts) !== "object") {
        opts = null;
    }
    FB.getLoginStatus(function(statusResponse) { //check login status
        if(statusResponse.status === 'connected') { //user logged in
            if(opts !== null && opts.scope !== null) {
                //if there is scope opt check that user grant access to all permissions
                FB.api('/me/permissions?fields='+opts.scope, 'get', {access_token : statusResponse.authResponse.accessToken }, function(response) { 
                    var perms = opts.scope.split(',');
                    for(var i=0;i<perms.length;i++) {
                        if(response.data[0][perms[i]] !== 1) {
                            //one of permissions fails - do login and break loop
                            FB.login(function(loginResponse) {
                                if(loginResponse.status === 'connected')
                                    proceedLogin(loginResponse.authResponse,callback); //ajax call
                            }, opts);
                            return;
                        }
                    }
                    //every perm is granted, great so let's proceed user login
                    proceedLogin(statusResponse.authResponse,callback);//ajax call
                });
            } else {
                //there's no opts or no perms to check
                proceedLogin(statusResponse.authResponse,callback);//ajax call
            }
        } else { //user not logged in :(
            FB.login(function(loginResponse) {
                if(loginResponse.status === 'connected')
                    proceedLogin(loginResponse.authResponse,callback);//ajax call
            }, opts);
        }
    });
}
function proceedLogin(authResponse,callback) {
    $.post('/ajax/login', authResponse, function(data) {
        if (data.user && callback && typeof(callback) === "function") {
            callback();
        }   
    });
}

这是PHP方面

function login() {
    //FB() your facebook instance
    FB()->setAccessToken( $_POST['accessToken'] ); //set access token from JS
    FB()->setAccessToken( FB()->setExtendedAccessToken() );
    $user = FB()->getUser();
    echo json_encode(array( 'user' => $user ));
}