检查用户是否登录而不询问权限 - 脸书

时间:2012-08-07 12:15:14

标签: php facebook api sdk permissions

我在Facebook上创建了一个应用程序,但在我要求权限之前,我想检查用户是否已登录并执行某些任务。

我试过了:

    $facebook   = new Facebook(array(
            'appId'  => $app_id,
            'secret' => $secret,
            'cookie' => true
        ));


    $user = $facebook->getUser();
    if ($user) {
      echo 'logged in';
    } else {
      echo 'not logged in';
    }

但它总是要求权限。

1 个答案:

答案 0 :(得分:1)

我使用了这段代码:

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


    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
                    //User Is Logged In and authorized
            var uid = response.authResponse.userID;
        } else if (response.status === 'not_authorized') {
            //User is Logged in and not authorized
        } else {
            // the user isn't logged in to Facebook.
        }
    });
};
</script>