PHP / Graph API:为什么我的facebook会话不起作用?

我是Facebook Graph API的新开发者.我已经按照本教程:
https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0.我测试了一次,它的工作原理.我有修改和重新测试,这是工作.但是,第二天,没有…我的facebook会话令牌有变化,我不知道为什么以及如何.

有我的代码:

// ALL includes and variables

// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( $app_ID, $app_secret );

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $url );

// generate login url with scope, each permission as element in array
$loginUrl = $helper->getLoginUrl( array($access_right) );

// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
    // 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;
        }
    } catch ( Exception $e ) {
         // catch any exceptions
         $session = null;
    }
}  

if ( !isset( $session ) || $session === null ) {
   // no session exists

   try {
       $session = $helper->getSessionFromRedirect();
   } catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
      // handle this better in production code
      print_r( $ex );
   } catch( Exception $ex ) {
      // When validation fails or other local issues
      // handle this better in production code
      print_r( $ex );
   }

}

// see if we have a session
if ( isset( $session ) ) {

// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );

// graph api request for user data
$graphObject = (new FacebookRequest( $session, 'GET', '/me/accounts'))->execute()->getGraphObject()->asArray();

// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

$page_token = $graphObject['data'][0]->access_token;

echo '<p>Page token : '.$page_token.'</p>';

$session = new FacebookSession($page_token);

/* make the API call */
$request = new FacebookRequest(
  $session,
  'POST',
  '/'.$pageID.'/feed',
  array (
    'message' => 'This is a test message',
  )
);
$response = $request->execute();
$graphObject = $response->getGraphObject();

/* handle the result */
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';

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

我的目标只是在我的粉丝页面上添加一条消息,它第一次运行.为什么变量“$_SESSION [‘fb_token’]”现在为NULL?我怎么能让它发挥作用?

谢谢你的回复!

编辑1:
我发现我的会话已将CSRF标题更改为:’fb_token’为’FBRLH_state’.关于这种变化的想法?

最佳答案 这是我使用最新的Facebook PHP-SDK 4.0.9登录用户的文件的内容(我认为).

<?php 
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' );

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;

if (!isset($_SESSION['facebookUserId']) || !isset($_SESSION['facebookSession']) || !isset($_SESSION['facebookUserProfile'])) {
    // init app with app id (APPID) and secret (SECRET)
    FacebookSession::setDefaultApplication($appId,$appSecret);

    // login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper( $canvasUrl );
    try {
         $FBSession = $helper->getSessionFromRedirect();
    } catch( FacebookRequestException $ex ) {
        // When Facebook returns an error
    } catch( Exception $ex ) {
         // When validation fails or other local issues
    }
    if (!isset($FBSession)) {
        // STEP ONE - REDIRECT THE USER TO FACEBOOK FOR AUTO LOGIN / APP APPROVAL
        header('Location: ' . $helper->getLoginUrl());
        exit();
    } else {
        $user_profile = (new FacebookRequest($FBSession, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
        $_SESSION['facebookUserId'] = $user_profile->getID();
        $_SESSION['facebookSession'] = $FBSession; // I DON'T THINK THIS ACTAULLY WORKS RIGHT
        $_SESSION['facebookUserProfile'] = $user_profile;

    }
}

我只是包含这个文件,如下所示.

include ('SimpleFacebookLogin.php');

我尝试保存会话,它在会话中没有正确保存,所以我想我也错过了一些东西,但我可以在创建会话后成功登录.我不知道如何再次访问它.我希望这有帮助.

点赞