如何验证访问令牌并在无效时获取新的访问令牌?
我的应用程序工作正常,一切正常,但是当用户注销或两小时后返回或更改密码或删除应用程序然后尝试再次添加它时,我会收到如下错误,其中包含每个错误的原因:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用户注销时清空 cookie 或会话?
Empty the cookies or sessions when the user logs out?