facebook SDK 4.0在php中无限重定向循环

时间:2014-07-25 08:50:48

标签: php facebook session facebook-javascript-sdk facebook-php-sdk

我在facebook sdk 4.0上遇到了问题

清除会话/ cookie后,它可以正常工作。但有时,我无法确定何时,如果我去应用程序,它会启动无限重定向循环!

我把所有代码放在git上,因为文档没有提供完整的答案:

https://github.com/sandrodz/facebook-canvas-app-sample-sdk-4.0/blob/master/index.php

<?php

// Working canvas APP, FB SDK 4.0

session_start();

// Load SDK Assets
// Minimum required
require_once 'Facebook/FacebookSession.php';
require_once 'Facebook/FacebookRequest.php';
require_once 'Facebook/FacebookResponse.php';
require_once 'Facebook/FacebookSDKException.php';
require_once 'Facebook/FacebookCanvasLoginHelper.php';
require_once 'Facebook/GraphObject.php';
require_once 'Facebook/GraphUser.php';
require_once 'Facebook/GraphSessionInfo.php';

require_once 'Facebook/HttpClients/FacebookHttpable.php';
require_once 'Facebook/HttpClients/FacebookCurl.php';
require_once 'Facebook/HttpClients/FacebookCurlHttpClient.php';

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Faceboob\FacebookSDKException;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;

use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;



// Facebook APP keys
FacebookSession::setDefaultApplication('XXX','XXXXX');

// Helper for fb canvas authentication
$helper = new FacebookCanvasLoginHelper();



// see if  $_SESSION exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
    // create new fb session from saved fb_token
    $session = new FacebookSession($_SESSION['fb_token']);

    // validate the fb_token to make sure it's still valid
    try 
    {
            if (!$session->validate())
            {
                $session = null;
        }
    }
    catch (Exception $e)
    {
        // catch any exceptions
        $session = null;
    }
}
else
{
    // no $_SESSION exists
    try
    {
        // create fb session
        $session = $helper->getSession();
    }
    catch(FacebookRequestException $ex)
    {
        // When Facebook returns an error
        print_r($ex);
    }
    catch(\Exception $ex)
    {
        // When validation fails or other local issues
        print_r($ex);
    }
}



// check if 1 of the 2 methods above set $session
if (isset($session))
{
    // Lets save fb_token for later authentication through saved $_SESSION
    $_SESSION['fb_token'] = $session->getToken();

    // Logged in
    $fb_me = (new FacebookRequest(
      $session, 'GET', '/me'
    ))->execute()->getGraphObject();

    // We can get some info about the user
    $fb_location_name = $fb_me->getProperty('location')->getProperty('name');
    $fb_email = $fb_me->getProperty('email');
    $fb_uuid = $fb_me->getProperty('id');
}
else
{
    // We use javascript because of facebook bug https://developers.facebook.com/bugs/722275367815777
    // Fix from here: http://stackoverflow.com/a/23685616/796443
    // IF bug is fixed this line won't be needed, as app will ask for permissions onload without JS redirect.
    $oauthJS = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id=1488670511365707&redirect_uri=https://apps.facebook.com/usaidgeorgia/&scope=user_location,email';";
}

?>

我继续逐行调试,这些是我的发现:

// see if a existing session exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
    echo '$_SESSION and $_SESSION["fb_token"] are set';

    // create new session from saved access_token
    $session = new FacebookSession($_SESSION['fb_token']);

    // validate the access_token to make sure it's still valid
    try 
    {
        if (!$session->validate())
        {
            $session = null;
            echo 'access_token is not valid';
        }
        echo 'access_token is valid';
    }
    catch (Exception $e)
    {
        // catch any exceptions
        $session = null;
        echo 'something error happened ' . $e;
    }
}

我收到错误:

$_SESSION and $_SESSION["fb_token"] are setsomething error happened exception 'Facebook\FacebookSDKException' with message 'Session has expired, or is not valid for this app.' in /home2/nakaidze/public_html/mesamoqalaqo_app/Facebook/FacebookSession.php:247 Stack trace: #0 /home2/nakaidze/public_html/mesamoqalaqo_app/Facebook/FacebookSession.php(221): Facebook\FacebookSession::validateSessionInfo(Object(Facebook\GraphSessionInfo), '148867051136570...') #1 /home2/nakaidze/public_html/mesamoqalaqo_app/user-functions.php(56): Facebook\FacebookSession->validate() #2 /home2/nakaidze/public_html/mesamoqalaqo_app/index.php(2): require('/home2/nakaidze...') #3 {main}

1 个答案:

答案 0 :(得分:1)

您在$_SESSION['fb_token']中使用的访问令牌已过期。默认情况下,Facebook返回的访问令牌持续2小时,然后过期。

首次获得FacebookSession后,您需要扩展其返回的访问令牌并将其保存在$_SESSION['fb_token']中:

$session = $helper->getSession();
$accessToken = $helper->getAccessToken();
$longLivedAccessToken = $accessToken->extend();
$_SESSION['fb_token'] = (string) $longLivedAccessToken;

此外,当您使用validate()验证访问令牌时,如果访问令牌无效,则会抛出:

// validate the access_token to make sure it's still valid
try 
{
    $session->validate();
    echo 'access_token is valid';
}
catch (FacebookSDKException $e)
{
    $session = null;
    echo 'Access token is no longer valid, need to get a new token';
}

这可能有助于澄清有关Facebook access tokens的信息。