如何验证访问令牌并在无效时获取新的访问令牌?

发布于 2024-12-07 09:18:57 字数 1507 浏览 0 评论 0原文

我的应用程序工作正常,一切正常,但是当用户注销或两小时后返回或更改密码或删除应用程序然后尝试再次添加它时,我会收到如下错误,其中包含每个错误的原因:

Fatal error: Uncaught OAuthException: Error validating access token: The session is invalid because the user logged out. thrown in /home/user/public_html/new_fb_app/src/base_facebook.php on line 1028

另外,如果我在出现此错误后尝试刷新,该应用程序将正常工作。所以我想这与应用程序尝试使用最后一个访问令牌(无效)有关。

这是我的代码:

<?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>';
}
?>

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 session 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>';
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2024-12-14 09:18:57

用户注销时清空 cookie 或会话?

Empty the cookies or sessions when the user logs out?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文