Facebook SDK 4 - 会话已过期,或对此应用无效

时间:2014-07-07 19:29:56

标签: facebook facebook-sdk-4.0

我试图创建一个捕获用户令牌然后从api检索数据的php页面,一切运行良好,直到我从我正在测试的facebook帐户手动删除应用访问,现在它给我一个错误,提到在标题中。

这是我的代码:

<?php

session_start();

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );


use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;


$id = '';
$secret = '';

FacebookSession::setDefaultApplication($id, $secret);

$helper = new FacebookRedirectLoginHelper('http://localhost/facebook/index.php');


try{
$session = $helper->getSessionFromRedirect();
}catch(Exception $e)
{

}

if(isset($_SESSION['token'])){

$session = new FacebookSession($_SESSION['token']);
try{
$session->Validate($id, $secret);
}catch(FacebookAuthorizationException $e){
$session = '';
}

}

if(isset($session)){
$_SESSION['token'] = $session->getToken();
echo "Login Successful<br>";
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$name = $response->getGraphObject()->asArray();
echo '<pre>' . print_r( $name , 1 ) . '</pre>';
echo '<a href="' . $helper->getLogoutUrl( $session, 'localhost/facebook/logout.php' ) .           '">Logout</a>';

}
else
{
$permissions = array('email','user_friends');
echo '<a href="' . $helper->getLoginUrl($permissions) . '">Login With Facebook</a>';

}
?>

1 个答案:

答案 0 :(得分:0)

OP解决方案。

通过用以下代码替换部分代码来解决问题:

if (isset($_SESSION) && isset($_SESSION['fb_token'])) {
    $session = new FacebookSession($_SESSION['fb_token']);
    try {
        if (!$session->validate()) {
            $session = null;
        }
    } catch (Exception $e) {
        $session = null;
    }
} else {
    try {
        $session = $helper->getSessionFromRedirect();
    } catch (FacebookRequestException $ex) {
    } catch (Exception $ex) {
        echo $ex->message;
    }
}