user6964
user6964

Reputation: 301

I can't get the session of user facebook, getUser return 0 - codeigniter

Regards,

I have a problem with Facebook SDK-PHP and Codeigniter.

I do not get the user's session when I try to capture and obtain basic data for that user.

The error I get is: "An active access token must be used to query information about the current user."

And when I apply the "getUser ()", I get "0".

NOTE: I download the php-sdk and put on my directory libraries the files base_facebook.php and facebook.php

In my application/config/config.php I put the key and app_secret of my facebook application.

Config.php :

$config['base_url'] = 'http://localhost/myapp_local';
$config['app_id'] = 'xxxxxxxxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_name'] = 'http://apps.facebook.com/myapp_local/';

In my Controller Main.php i have the follow code:

class Main extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $fb_config = array(
            'appId' => $this->config->item('app_id'),
            'secret' => $this->config->item('secret')
        );
        $this->load->library('facebook', $fb_config);

    private function _getInfoUserFB() {
        try {
            $fb_id = $this->facebook->getUser();
            if ($fb_id) {
                $fb_data = $this->facebook->api('/me');
                return $fb_data;
            }
        } catch (Exception $exc) {
            echo $exc;
        }
    }
       [....]
}

Now, in my variables $fb_id , where obtain the bool value, always i have "0". So, when i try to get the data of the current user facebook always i have a Exception with the message: "An active access token must be used to query information about the current user."

$fb_data['id'] = NULL
$fb_data['first_name'] = NULL
$fb_data['last_name'] = NULL

What i doing wrong here? Any step or setting I miss?

NOTE 2: I don't have some button Login with Facebook, my app connect directly to facebook via the configuration of my app on facebook. (Canvas URL, Secure Canvas URL, Canvas Page, Site URL)

Note 3: My application have some permission that need require, So I put the necessary permissions on Settings-Permissions BUT don't show me the option of "Auth Referrals" to show Dialog confirm permissions.

I use CodeIgniter version 2.1.3 and PHP-SDK v 3.2.0

Upvotes: 0

Views: 3939

Answers (3)

user6964
user6964

Reputation: 301

OK!, i resolved it. The fixed was the interaction with the newer version have to this form: http://www.galalaly.me/index.php/2012/04/using-facebook-php-sdk-3-with-codeigniter-2-1/

And then, you must be call the function that obtain the information of current user for first time when your app load for first time. In other words, you call the function in the MAIN, INDEX view of your website.

If you call in other time, your function can't obtain the access_token of current user.

Upvotes: 0

zer02
zer02

Reputation: 4021

You have to go to Facebook Developer Site -> Apps -> Permission. And set the basic info permission for your app.

Then in your function getLoginUrl(array("scope" => "user_about_me))), you have to give the parameters.

Finally you have to save the access token $this->facebook->getAccessToken();

If you have problems you can ask me. I have setup my Codeigniter with nearly all Social Media Platforms :)

Upvotes: 1

Eduard Luca
Eduard Luca

Reputation: 6611

Facebook does really bad with localhost. I've experienced this several times too. The solution I've found, which lets me develop locally, is to edit my hosts file (C:\Windows\system32\drivers\etc\hosts or /etc/hosts), and add something like the following line in there:

127.0.0.1    myfbapp.local

Using this, if you point your browser to http://myfbapp.local/, it will actually go to 127.0.0.1 and that means localhost. Of course, you would have to update your CI config too, and change your base URL to http://myfbapp.local.

As far as I can tell, the Facebook PHP SDK is having trouble setting cookies if there is no dot (.) in the domain name, so that's exactly what my solution does.

Upvotes: 0

Related Questions