nourdine
nourdine

Reputation: 7597

Expired authorization code in PHP facebook SDK

I am using the new 4.* facebook php SDK and I am having an issue! ;)

Basically I am trying to find a way to make requests to the graph api after the first redirect when the user is sent back to my site after logging in into facebook. In that case I do (in my index.php):

$h = FacebookRedirectLoginHelper("blablabla");
$request = new FacebookRequest($h->getSessionFromRedirect(), 'GET', '/me');
$me = $request->execute()->getGraphObject();

and it works perfectly and I can print the date about the logged user.

My problem is that if I reload (f5) the page index.php then I get an exception:

Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message 'This authorization code has expired.' in /home/nourdine/development/faisbuk/vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequestException.php:87 

So my question is: where am I supposed to get a new authorization code in order to make new graph api calls with the authenticated user even in pages that are not executed as a result o the facebook login process?

Thanks

Upvotes: 4

Views: 4522

Answers (2)

Ahmet
Ahmet

Reputation: 812

Concretely speaking, this piece of code is what needs to be implemented:

    // see if we have a session in $_Session[]
if( isset($_SESSION['token']))
{
    // We have a token, is it valid? 
    $session = new FacebookSession($_SESSION['token']); 
    try
    {
        $session->Validate($appid ,$secret);
    }
    catch( FacebookAuthorizationException $ex)
    {
        // Session is not valid any more, get a new one.
        $session ='';
    }
}

Full working sample is available at this page: Facebook SDK 4.0.0 for PHP: A working sample to manage sessions.

Upvotes: 1

WizKid
WizKid

Reputation: 4908

You don't need a new authorization code. What you need is to use the same session. $h->getSessionFromRedirect() returns a session. That session you want to use next time also.

Upvotes: 3

Related Questions