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:
session_start();
FacebookSession::setDefaultApplication( $app_ID, $app_secret );
$helper = new FacebookRedirectLoginHelper( $url );
$loginUrl = $helper->getLoginUrl( array($access_right) );
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
$session = new FacebookSession( $_SESSION['fb_token'] );
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
print_r( $ex );
} catch( Exception $ex ) {
print_r( $ex );
}
}
if ( isset( $session ) ) {
$_SESSION['fb_token'] = $session->getToken();
$session = new FacebookSession( $session->getToken() );
$graphObject = (new FacebookRequest( $session, 'GET', '/me/accounts'))->execute()->getGraphObject()->asArray();
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);
$request = new FacebookRequest(
$session,
'POST',
'/'.$pageID.'/feed',
array (
'message' => 'This is a test message',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';
} else {
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?