duanji1902 2013-07-06 12:03
浏览 73

使用CodeIgniter + Facebook PHP SDK时:getUser()始终返回0

I am attempting to use the Facebook PHP SDK in conjunction with CodeIgniter to allow users to login to my site using Facebook Connect. No matter what I try, getUser() always returns 0, even after (apparently) successful authentication via Facebook.

CodeIgniter version: 2.1.3

Facebook PHP SDK version: 3.2.2

I have created a config file, facebook.php, in the application/config folder and I am loading the Facebook PHP SDK via CodeIgniter's $this->load->library(...) method. The library is indeed getting loaded and I can successfully call many of the get...() methods including getAccessToken(), getAppId() and getAppSecret(), all of which return their expected values.

Here is a stripped down version of my login controller: (note that I also provide an alternate method of logging in via email, hence the CodeIgniter session code sprinkled throughout)

class Login extends CI_Controller {

    public function __construct()
    {
        //Call parent constructor
        parent::__construct();

        //Magic sauce - not sure if this is required but a lot of other people
        //are recommending it to be included (happy to remove it if necessary)
        parse_str($_SERVER['QUERY_STRING'], $_REQUEST);

        //Load facebook library
        $facebook_config = $this->load->config('facebook');
        $this->load->library('facebook', $facebook_config);
    }

   public function index()
    {
        //Check if user is logged in
        $user_id = $this->session->userdata('user_id');
        $is_logged_in = $this->session->userdata('is_logged_in');

        if(($is_logged_in) && ($user_id != 0)) {
            //Logged in - redirect to game
            redirect('game');
        } else {
            //Not logged in
            //Get facebook login url
            $facebook_data = array(
                'redirect_uri' => 'hxxp://xxxxxxxx.com/facebook_login/',
                'scope' => 'email'
            );
            $data['facebook_login_url'] = $this->facebook->getLoginUrl($facebook_data);
            //Redirect to login form
            $this->load->view('login/login_form', $data);
        }
    }

    public function facebook_login()
    {
        //Always returns 0!! Even after authenticating via facebook!
        $facebook_user_id = $this->facebook->getUser();

        if ($facebook_user_id) {
            try {
                $user_profile = $this->facebook->api('/me');
                print_r($user_profile);
            } catch (FacebookApiException $e) {
                echo $e->getMessage();
            }
        } else {
            echo "Could not log in with Facebook";
        }
    }
}

The stripped down view (login_form.php) is as follows:

<html>
    <head>
        <title>Facebook Connect Test</title>
    </head>
    <body>
        <a href='<? echo $facebook_login_url; ?>'>Login with Facebook</a>
   </body>
</html>

I have a route that redirects hxxp://xxxxxxxx.com/facebook_login to the login/facebook_login method, which is working.

I am running this code on a live development server.

My current flow is as follows:

  1. Load hxxp://xxxxxxxx.com/ (Routes to login controller, which loads login_form view)
  2. Click "Login with Facebook" link
  3. Facebook asks me to login (which I do)
  4. Facebook asks me to give permission to my app (which I do)
  5. Facebook redirects me to the url specified in the redirect_uri parameter, which is identical to the one on the app settings page

And here's where it all goes wrong. The $this->facebook->getUser() method ALWAYS returns 0, even after authentication.

I have been scouring the Facebook developer documentation and everywhere else on the internet I can think of trying to find an answer to this. I have come across many posts similar to this and have tried to apply the solutions suggested, but to no avail.

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • douluxia0606 2013-07-12 23:42
    关注

    I managed to solve my problem. The questions linked to by Qweick and Stéphane Bruckert had the solution. The problem lies in the getCode() function of the base_facebook.php file.

    The getCode() function needs to be modified. The modifications I used are listed below.

    Existing non-working code:

    protected function getCode() {
        if (isset($_REQUEST['code'])) {
            if ($this->state !== null &&
                    isset($_REQUEST['state']) &&
                    $this->state === $_REQUEST['state']) {
    
                // CSRF state has done its job, so clear it
                $this->state = null;
                $this->clearPersistentData('state');
                return $_REQUEST['code'];
            } else {
                self::errorLog('CSRF state token does not match one provided.');
                return false;
            }
        }
    
        return false;
    }
    

    Modified working code:

    protected function getCode() {
        $server_info = array_merge($_GET, $_POST, $_COOKIE);
    
        if (isset($server_info['code'])) {
            if ($this->state !== null &&
                    isset($server_info['state']) &&
                    $this->state === $server_info['state']) {
    
                // CSRF state has done its job, so clear it
                $this->state = null;
                $this->clearPersistentData('state');
                return $server_info['code'];
            } else {
                self::errorLog('CSRF state token does not match one provided.');
                return false;
            }
        }
    
        return false;
    }
    

    The getUser() call now returns a valid user Id and the Facebook API calls now return valid data.

    Thanks to everyone that helped point me in the right direction!

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)