PHP / Graph API: Why is my facebook session not working?

I am a new developer of the Facebook API. I follow this guide: https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0 . I have a test once and it works. I have a modification and retest, and it works. But the next day, nothing ... My facebook token has changed, and I don’t know why and how.

There is my code:

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

My goal is simply to send a message to my fans page, and it works for the first time. Why is the variable "$ _SESSION ['fb_token']" now NULL ?? How can I make it work?

Thanks for the answer!

Edit 1: I found that my session changed the name of CSRF: 'fb_token' to 'FBRLH_state'. The idea of ​​this change?

+4
2

, , , PHP 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');

, , , - , . , . , .

+3

Actualy ! =) script. session_start() session_write_close() - , ?

-1

Source: https://habr.com/ru/post/1547042/


All Articles