开发者

How to validate the Access Token and get new one if it's invalid?

My app is working just fine and everything is OK but when the user logged-out or come back after two hours or change his password or remove the app then try to add it again I get an error like this with the reason of each error :

Fatal error: Uncaught OAuthException: Error validating access token: The sessi开发者_JS百科on is invalid because the user logged out. thrown in /home/user/public_html/new_fb_app/src/base_facebook.php on line 1028

Also, if I tried to refresh after this error came up the app will work just fine. So I guess it has something to do with the app tried to work with the last access token (which was invalid).

This is my code :

<?php
$user = null; //facebook user uid
try{
include_once "src/facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
    }
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'       => 'my appId',
  'secret'      => 'my app secret',
  'fileUpload'  => true
));

// Get User ID
$user       = $facebook->getUser();
$loginUrl   = $facebook->getLoginUrl(
    array(
    'scope' => 'read_stream, publish_stream, photo_upload, user_photos'
    )
);

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e);  // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
    }
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>


Empty the cookies or sessions when the user logs out?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜