Using FB SDK (PHP & Javascript Together) process OAuthException “active access token must be used to query information”-Collection of common programming errors

Like many people I’ve found across stack overflow I’ve been experiencing an issue where after about 10-15 mins of time the users get randomly logged out and brought back to the “Login with Facebook” page where the button does not work without a press and a manual refresh.

I setup the try { } of the getUser() to log any eception and what I found was the general error:

OAuthException: An active access token must be used to query information about the current user.

I quickly searched the net trying find a solution for this error of miss matched information. I did try the solutions of checking for $facebook->getUser() instead of facebook->api('/me') as well as alway adding the Access Token to requests but clearly there is still something wrong.

I did change the $facebook->api('/me') call to $facebook->api('/'.$FacebookUserID) and that worked for a while about an hour and then the user would see a different error:

PHP Fatal error:  Uncaught Exception: 102: Requires user session thrown in /base_facebook.php on line 1249

Which is pretty much the same error but instead of bringing me back to the login screen it brought me to a PHP error page.

Does anyone have any ideas or advice on how to fix this issue?
Thank You!

Here is the code I am using (broken down in a MVC setup)
(1) The PHP Controller loads the PHP SDK upon loading of the website and sets $fBSdk = $fb->fBSdk() which calls the method below.

public function fBSdk() {
  $this->facebook = new Facebook(array(
    'appId'  => '1234567890',
    'secret' => 'asdf123qwerty', 
    'cookie' => true,
  ));
  $this->user = $this->facebook->getUser(); //Store "$user" in FB Class
  if ($this->user) {
    try {
      $this->user_profile = $this->facebook->api('/me');
    } catch (FacebookApiException $e) {

      //$this->user = null; user) {
    return true;
  }
  else {
    return false;
  }
}

(2) The $fBSdk variable is then checked to see if it is === false, which is is and the login button is loaded as well as the Javascript SDK

if ($fBSdk === false) {
    //[... Code for the LOG IN Page & Button ...]
    echo 'Login with Facebook';
}

(3) The Login page loads including the Javascript SDK:



  window.fbAsyncInit = function() {
    FB.init({
      appId: '1234567890',
      channelUrl: '//www.someDomain.com/dev/channel.php', //Yes this is the correct Link
      status: true,
      cookie: true,
      xfbml: true,
      oauth: true,
    });

    //Used to Reload AFTER login (doesn't fire with above issue)
    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload();
    });
    FB.Event.subscribe('auth.logout', function(response) {
      window.location.reload();
    });

    FB.Canvas.setSize({ width: 810 });
    FB.Canvas.setAutoGrow();
  };

  (function(d, debug){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
     ref.parentNode.insertBefore(js, ref);
   }(document, /*debug*/ false));

(5) The user information is used with in the app in various places the most common and model example, as they are all used pretty close is getID() and getFirstName() who has code that look like:

//Note: $this->user_profile is in the FB class and
//      is set in set on Line 7 of Example 1 above.
public function getID() {
  return $this->user_profile['id'];
}

(6) The user then clicks the login button and controller is fired again, the $fBSdk variable is checked again and the variable is set to true thus firing the else { ... } statement.

Thanks!

UPDATE 1:
Back to square 1, it seems nothing is really working so back to the place I got the code, the php sdk: https://github.com/facebook/facebook-php-sdk/blob/master/examples/with_js_sdk.php

UPDATE 2:
So, I get the same error in the same of code that I get in my app, about 10 mins and it times out. weird. I found the issue in facebook bugs but cannot find a fix. https://developers.facebook.com/bugs/404450666302585

Thoughts?

Originally posted 2013-11-30 17:54:07.