2

Possible duplicate of facebook Uncaught OAuthException: An active access token must be used to query information about the current user

But I did not get a proper solution.

I am using facebook sdk with codeigniter it is working properly. but sometimes it throws exception OAuthException: An active access token must be used to query information about the current user.

Here is the code of Controller file

$config = array(
                    'appId'  => 'XXXXXXX',
                    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
                    'fileUpload' => true,
              );

$this->load->library('facebook/Facebook', $config);

$fbme = null;

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

if ($user) {
    try {               
        $fbme = $this->facebook->api('/me');
    } catch (FacebookApiException $e) {
          error_log($e);
          $user = null;
    }
}

//if user is logged in and session is valid.
if ($fbme){
              //do some stuff...
              //redirecting to user home page (my site's)
    }

In view file, I am using js sdk. Code for view file is

<div id="fb-root"></div>
    <script type="text/javascript">
        var button2;

        window.fbAsyncInit = function() {
            FB.init({ appId: 'XXXXXXXX',
                status: true,
                cookie: true,
                xfbml: true,
                oauth: true});

           function updateButton(response) {
                button1 = document.getElementById('fb-login'); //login button
                button2 = document.getElementById('fb-logout'); //logout button
                if (response.authResponse) {
                    //user is already logged in and connected
                                button2.onclick = function() {
                                    FB.logout(function(response) {});
                                };
            } else {
                    //user is not connected to your app or logged out
                    button1.onclick = function() {
                        FB.login(function(response) {
                            if (response.authResponse) {
                            } else {
                                //user cancelled login or did not grant authorization
                            }
                        }, {scope:'email,user_birthday,user_about_me,user_likes,user_interests,user_education_history,user_work_history'});
                    }
                }
            }

            // run once with current status and whenever the status changes
            FB.getLoginStatus(updateButton);
            FB.Event.subscribe('auth.login', function(response) {
                window.location.reload();
            });
            FB.Event.subscribe('auth.logout', function(response) {
                window.location.reload();
            });
        };
        (function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol
                + '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        }());       
    </script>

PROBLEM:

Sometimes the it throws "OAuthException: An active access token must be used to query information about the current user." to the error log file.

Case: user is logged in facebook and has already authenticated my app If a user is logged in facebook and authenticated my app and tries to logged in to my site, rather redirecting the user to my site (php sdk is not working in this case), it is loading the view file. In view file also I am checking whether the user is logged in in facebook or not. (in view) if the user is logged in it will reload the page, same controller script will run and it redirects the user to user's home page(php sdk is working in this case.).

But first time it is not working. Don't know where I am making the mistake.

Also that logout button (in view file) is also sometimes not working. Means on clicking it is not logging out the user from facebook. (It is only happening in one of my colleague's browser. She is using chrome in windows 7)

1 Answer 1

5

I was having a similar problem while using PHP-SDK + JS-SDK together. I temporarily solved it by removing the FB.init "cookie:true" option.

By some reason that I couldn't find, an invalid signedRequest cookie is being shared between PHP and JS, then, the valid access_token of the PHP Session was being override with an invalid access_token, thus, destroying the previously valid PHP token.

The difference between my problem and yours is that you're logging via JS and trying to access the same session on PHP (I was doing it backwards, logging on PHP and trying to use the JS-SDK).

This bug is apparently fixed on this pull request: https://github.com/facebook/facebook-php-sdk/pull/48

1
  • FYI: The signed_request is not “invalid” – but the SDK tries to exchange the code value inside of it for an access token every time you make a new request, and that leads to an error after the recent changes (Dec. 6th 2012)
    – CBroe
    Dec 10, 2012 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.