Hey all,
I'm currently using the PHP Facebook SDK for my project (http://github.com/facebook/php-sdk/). I have set it up so that a user will have to click a link to visit Facebook, which will then verify the user to use the application, and then return the session details to my site, which is fine. However, I want to store these session details within a database so that the user on logging in, can access information from their account at any time. Currently, if the user logs out of Facebook, and then tries to retrieve information from my site, I get the following exception "OAuthInvalidTokenException: Error processing access token." If the user is logged into Facebook on the same PC, then there isn't an issue.
Is there anything that I have to set, or is there some other way I should be performing this connectivity to be able to retrieve the data when the user isn't logged in?
The code I use to get/set the Facebook session is:
require 'libraries/facebook/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
//Save to database
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl();
header('Location: ' . $loginUrl);
}
Once I have the session saved to the database from this part, I display the data by posting the database serialized session to this:
$session = unserialize($_POST['session']);
require_once('libraries/facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
));
$facebook->setSession($session, false);
try {
$uid = $facebook->getUser();
// Do stuff
} catch (FacebookApiException $e) {
echo "<tr><td>" . $e . "</td>";
}
Is there anything glaringly wrong with what I'm doing here? Or can I not use this method to create an oAuth connection that lets me request data for users when they are not logged in?
Thanks,
Dan