Facebook query failing in PHP – access token problems-Collection of common programming errors

I’m playing around with FQL with a Facebook iFrame app. I have just started getting an intermittent error recently when I attempt a simple FQL query such as get an array of the user’s friends… I get the following error message:

Fatal error: Uncaught Exception: 102: Requires user session thrown in base_facebook.php line 1039

The only thing that appears to clear this message is clearing all of my cookies. Simply logging out and back in to Facebook does not stop it. The frustrating thing is I that I am unable to repeat the error on demand which is making it hard to track down what is going on but from looking at that line on base_facebook.php it appears as though the exception was thrown because the access token is no longer valid

How do I account for this? i.e. How can I check whether the access token is still valid or not and if not request/set a new one? I’m using the latest Facebook PHP SDK v3 with CodeIgniter framework.

There was a similar question but it related to the earlier version of the SDK so no longer valid.

EDIT: Tried a few other things… As the index page detects that the user is logged in it doesn’t redirect to the oauth link (i.e. https://www.facebook.com/dialog/oauth?client_id-etc.) . As a test I tried placing that link on the page anyway to try clicking and force new authentication. That didn’t work either. I’ve tried setting access token to null and then getting a new one. Also no luck there. The only fix still is to clear all cookies in the browser.

Any links to info/tips on how to debug/solutions etc. greatly appreciated as always!

Here’s an outline of the code:

public function index()
{
    $this->load->library('facebook'); 

    // check if we can retrieve user id from facebook -    

    $user = $this->facebook->getUser();

    if (!$user) 

    { // if no user id there then redirect to Facebook login page 

         $this->loginByFacebook();   
    } 
    else // if we have id then we have a logged in FB user
    {                           

      $fb_uid = $user;
      $fb_usr = $this->facebook->api('/me');

      print_r($fb_usr); // This always works and outputs my FB info

     // HERE I PUT A LINK TO NEW PAGE WHICH IS CREATED WITH FUNCTION BELOW

}

function getFriendsList () {

    //echo "here";
    $this->load->library('facebook'); 
    //$access_token = $this->facebook->getaccessToken(); 
    $query = '
        SELECT first_name, last_name, name, uid,pic_square FROM user
        WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me() ) 
        ORDER BY name';


         // I have tried running the query by getting access token and supplying it and without and still have the same problem.

    //$FQL = array ( "method" => "fql.query", "query" => $query, "access_token" => $access_token);
        $FQL = array ( "method" => "fql.query", "query" => $query);

    $data = $this->facebook->api($FQL);     

    return $data;           

}
  1. I’m going to close this as after carrying out some more tests I can’t appear to recreate the behaviour consistently. It seems to bring up the error when using Chrome on a Mac but not in other browsers and not so far in Chrome on a PC. A further test I did was to go into the Chrome settings and delete cookies from facebook.com only and still that did not clear it. Only clearing all cookies would cure the problem.

    The fact that no-one has offered any suggestions on this so far (and I’m unable to find any other references to it on the web) makes me wonder if this is just something affecting my particular set up.

  2. This appeared in my application after i added an access_token to my fql-query. i removed it again and the error remained. my problem SEEMS to have been that i called the function without a prior authentifcation (login-url). somehow it worked afterwards. hope this helps.

Originally posted 2013-11-19 13:17:51.