Facebook setExtendedAccessToken()和FB.Event.subscribe('auth.login')

时间:2013-04-12 16:56:06

标签: facebook

我正在开发我的第一个facebook应用程序。我将setExtendedAccessToken()函数与FB.Event.subscribe('auth.login')结合使用。

我的js-code看起来像这样:

window.fbAsyncInit = function() {

    // init the FB JS SDK
    FB.init({
      appId      : 'XXX', // App ID from the App Dashboard
      channelUrl : 'http://domain.tld/app/channel.php', // Channel File for x-domain communication
      frictionlessRequests: true,
      cookie: true, 
      xfbml: true
    });

    FB.Event.subscribe('auth.login', function(response) {
        $('#loginSpinner').show();
        $('#loginFacebook').hide();
        window.location = "http://domain.tld/app/?first_login";
    });      

};

auth.login事件只应在用户首次登录时触发。 此PHP代码也应仅在首次登录时触发:

// triggers on first login
if (isset($_GET['first_login'])) {
    if ($me) {
        $facebook->setExtendedAccessToken();
        $friends = $facebook->api('/me/friends');
        $friends = $friends['data'];
        if (!empty($friends)) {
        //sql
        }
        $permissions = $facebook->api("/me/permissions");

        if( array_key_exists('publish_actions', $permissions['data'][0]) ) {
            $attachment = array(
                    'message' => 'XXX',
                    'name' => 'XXX - App',
                    'link' => 'http://domain.tld/app/',
                    'description' => 'XXX',
                    'picture'=> 'http://domain.tld/app/img.jpg',
                    'access_token' =>  $facebook->getAccessToken()
            );
            $facebook->api('/me/feed', 'POST', $attachment);

            //sql
        }
    }
    header('Location: http://domain.tld/app/');
    exit;
}

问题是无限循环,它总是会调用auth.login,因为setExtendedAccessToken()会破坏当前会话,而facebook会自动将用户登录到我的应用程序。

有没有办法只触发auth.login一次?当我将setExtendedAccessToken()设置为评论时

// $facebook->setExtendedAccessToken();

我的网站只会将用户重定向一次。但是我需要为我的应用程序进行扩展登录。

我之前的问题:My WebApp (using facebook login) logs me out

1 个答案:

答案 0 :(得分:2)

那你为什么不这样做呢?

FB.Event.subscribe('auth.login', function(response) {
        //it will only redirect if the token expires in less than 02h13m20s
        if(response && response.authResponse.expiresIn < 8000){
        $('#loginSpinner').show();
        $('#loginFacebook').hide();
        window.location = "http://domain.tld/app/?first_login";
        }
    }); 
相关问题