webtechnick / CakePHP-Facebook-Plugin

CakePHP Facebook Plugin
http://facebook.webtechnick.com
445 stars 138 forks source link

FB::api('/me') does not work on FIRST page load #127

Closed vigorouscoding closed 11 years ago

vigorouscoding commented 11 years ago

EDIT: I'm using the current versions of CakePHP (2.3.1) and cloned this repo yesterday (March, 20th 2013).

FB::api('/me') does not work on FIRST page load - refreshing the page helps. I can't seem to put a finger on the why.

In FB.php the function "__callstatic" logs an error, but I don't know how to fix it. I tried redirecting once, so it would maybe have that token, but to no avail.

object(FacebookApiException) {
    [protected] result => array(
        'error' => array(
            'message' => 'An active access token must be used to query information about the current user.',
            'type' => 'OAuthException',
            'code' => (int) 2500
        )
    )
    [protected] message => 'An active access token must be used to query information about the current user.'
    [protected] code => (int) 0
    [protected] file => '/var/www/vhosts/epresia.ch/frontend/app/Plugin/Facebook/Vendor/base_facebook.php'
    [protected] line => (int) 1238
}

Anyone else facing the same issue ?

vigorouscoding commented 11 years ago

It seems the access_token is not present during the first page load.

echo FB::getAccessToken();
outputs this for every user I tested it with so far. (In case this is an access token of sorts I edited it slightly for anonymization)
182512728464031|f08c6880f70c39474b1a2a39aad7974f

Notice the "|" in there?

Reloading the page outputs a real access token.

vigorouscoding commented 11 years ago

Seems like I figured it out - I was reading the userinfo in the beforeFilter of AppController. After moving this to the UsersController this behavior stopped occurring...

Hope this may help someone else who is facing the same problem.

simonbuehler commented 11 years ago

in AppController

function beforeFilter(){
   $this->set("facebookuser", $this->Connect->user());
}

and home.ctp containing

if ($facebookuser){
    // do logged in stuff
    echo ($this->Facebook->logout()); 
  }else{
    echo $this->Facebook->login(array(
      'perms' => 'manage_pages,publish_stream'
    ));
  }

doesn't work anymore,

how did you fix that?

vigorouscoding commented 11 years ago

I'm not using this anymore, but IIRC I just moved the stuff I had in AppControllers beforeFilter to the specific controller I needed it in.

DColl commented 11 years ago

I found that the best way to deal with first-time comers along with a way to get updated on the user information (/me) is that : In AppController.php :

    public function beforeFilter(){
        if($this->Connect->FB->getUser() != 0){
            $this->set('facebookUser', $this->Connect->user());
        }

This way, first $this->Connect->FB->getUser() initialize the component, fills in FB->uid, etc. That way you get a 0 if user is not logged in Facebook OR as not granted access to your app. Both situations, you don't want to call ->api(/me) (throught Connect->user() ) and instead redirect your user to a welcome page or directly to the login dialog.

Note, that the important function to work with is FB->getUser(), which parse the ['signed_request'], etc. Connect->user() essentially just fill in the Session['FB'] array. For you to decide if this data is useful.

On several WorkController.php, I only use

    public function beforeFilter(){
        $this->Connect->FB->getUser();
    }

Connect component is initialized with data, from which Connect will retrieve Auth user and fill in Session['Auth']['User']['xxdetails'] . If you've got no need for specific Facebook details, and just need to deal with your database User[id], keep it simple ;)