BuddyJoe
BuddyJoe

Reputation: 71101

Facebook PHP SDK - OAuthException - OAuth2

How are you supposed to deal with people who signed into Facebook a while ago. Come to your site you should them a continue link (because you detect that they are already logged into Facebook) and then on the page you direct them to you get this error.

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /var/www/html/lib/base_facebook.php on line 1039

I don't understand how you are supposed to prevent this? Does this have something to do with the signed info that you give back to Facebook to get an access_token? Seems like this signed info can expire (it has an issued_at date). What is the correct way to handle this in your website's flow?

Are you expected to write code like this:

<?php 
$user = $facebook->getUser();
try {
  // attempt to do a call just to see if you are going to have this issue
  $profile = $facebook->api('/me'); 
} catch (Exception $e) {
  $user = false;
}
if ($user) { ?>
  <a href="start.php">Begin</a>
<?php } else { ?>
    <fb:login-button scope="email" size="large">Connect</fb:login-button>
<?php } ?>

Instead of this:

<?php 
$user = $facebook->getUser();
if ($user) { ?>
  <a href="start.php">Begin</a>
<?php } else { ?>
    <fb:login-button scope="email" size="large">Connect</fb:login-button>
<?php } ?>

Getting the $user back from the Facebook SDK only seems to tell you there is a cookie. And not if that will actually work when you go to do the API calls.

UPDATE: So my only problem with this method is ... when the user does have a cookie on my site, but the API call fails - I show them the connect button. User clicks the connect button, it quickly appears and disappears. Because it wasn't a true 'auth.login' that just occured, the user will not get sent to my start.php page via JavaScript redirect. How do others handle this? I'm stumped. Please tell me if there are other flaws with how I'm attempting to do this.

Upvotes: 0

Views: 1741

Answers (2)

Chaney Blu
Chaney Blu

Reputation: 343

Try passing the access token to the API call that verifies that the user has authorized your application. Below is what I do, and it should help to alleviate the OAuthException you're getting.

$user = $facebook->getUser();

if($user) {
    $access_token = $facebook->getAccessToken();
    $params = array('access_token' => $access_token);
    try {
        $me = $facebook->api("/me", $params);
    } catch(FacebookApiException $e) {
        $user = null;
    }
}

if($me) {
    // proceed with authenticated user with an active access token.
}

Upvotes: 1

Ashwini Dhekane
Ashwini Dhekane

Reputation: 2290

Are you expected to write code like this:

Yes, you will have to handle the OAuthException.

But rather than using FBML login button, you can redirect the user to facebook login url for your application. You can get the url by using function getLoginUrl provided by Facebook PHP SDK. See getLoginUrl for more information.

For your email permissions you can use following array:

$params = array( 'scope' => 'email' );

Once successfully logged in, the user will be redirected back to the application page which redirected him to facebook login.

Upvotes: 0

Related Questions