1

所以,我使用 FB 登录按钮让用户登录。单击 FB 登录按钮后,用户将被重定向到名为 fb_redirect.php 的文件,以便 php 可以启动会话。

window.fbAsyncInit = function() 
{
    FB.init({
        appId: '12345', 
        status: true, 
        cookie: true, 
        xfbml: true
    });
    FB.Event.subscribe('auth.login', function(response) 
    {
        window.location = '/fb_redirect.php';
    });
};

在 fb_redirect 文件中,我有以下内容:

session_start();
require("model/facebook_api/src/facebook.php");

$facebook = new Facebook(array(
    'appId' => '12345',
    'secret' => '12345'
));

$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);

if($access_token != "") 
{
    $user = $facebook->getUser();

    if($user != 0)
    {
        $user_profile = $facebook->api("/".$user);  
        $fb_id = $user_profile['id'];
        $fb_first_name = $user_profile['first_name'];
        $fb_last_name = $user_profile['last_name'];
        $fb_email = $user_profile['email'];

        $_SESSION['login'] = "true";
    }
}

header("Location:index.php");

文件中发生的事情有点令人困惑。它真的是命中或错过。如果我已经登录 FB,然后尝试使用 FB 登录按钮登录我的站点,则会弹出一个屏幕然后很快消失。但是,它不会让我登录到我的网站。

如果我没有登录 FB 并尝试登录我的网站,则会弹出一个屏幕并提示我登录 FB。当我在弹出屏幕中成功登录 FB 时,会发生重定向。但是,当它发生时,我要么收到一条错误消息,告诉我我需要一个有效的访问令牌。其他时候,它只是重定向到主页而不设置会话。所以,我猜根本没有设置访问令牌。

我在某处读到可能是您必须手动设置访问令牌。所以,我尝试了:

$facebook->setAccessToken($access_token);

但是,那没有用。

我不知道如何理解这一点。有任何想法吗?

4

1 回答 1

1

don't know if it helps (because, let's face it, FB SDK is quite versatile), but FB guidelines would rather you get the user before you get the access token; maybe (just a theory) when you're already logged in, the access token is not available to your $facebook object until you execute the getUser() method...seems reasonnable, i guess, to get the user before getting his access token.

于 2013-05-30T15:34:30.467 回答