Symfony2 session and PHP SDK 4.0 problems on login
I am trying to integrate Facebook PHP-SDK 4 and Symfony2
I installed Facebook SDK using composer and used basic code from Facebook developer documentation
Create login url
public function indexAction(Request $request) {
//Generate
FacebookSession::setDefaultApplication('appId', 'secret');
$redirectUrl = $this->generateUrl('auth_facebookredirect');
$helper = new FacebookRedirectLoginHelper($redirectUrl);
$helper->disableSessionStatusCheck();
$loginUrl = $helper->getLoginUrl();
return $this->render('UserBundle:FB:index.html.twig', array('loginUrl'=>$loginUrl));
}
Callback or Facebook redirect after successful login
public function successAction(Request $request) {
FacebookSession::setDefaultApplication('appId', 'secret');
$redirectUrl = $this->generateUrl('auth_facebookredirect');
$helper = new FacebookRedirectLoginHelper();
//$session = $helper->getSessionFromRedirect();
try {
$session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
echo " When Facebook returns an error";
} catch (\Exception $ex) {
echo " When validation fails or other local issues";
}
if ($session) {
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
var_dump($me);
}
return $this->render('UserBundle:FB:success.html.twig', array());
}
At first I got the error
FacebookSDKException: Session not active, could not store state.
I came across this link and it says there is a session problem and is using
$helper->disableSessionStatusCheck();
because of this login worked, but when I got redirect to Facebook callback url / redirect url after login to FB I got an exception which
DefaultApplication is not set
so i added FacebookSession::setDefaultApplication('appId', 'secret');
insuccessAction
I am still getting the error after logging in in the section below, I cannot get the session here.
try {
$session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
echo " When Facebook returns an error";
} catch (\Exception $ex) {
echo " When validation fails or other local issues";
}
I am getting error here catch (\Exception $ex) {
echo " When validation fails or other local issues";
}
$session = helper->getSessionFromRedirect();
what is causing the error here, my Facebook login credentials are correct.
I cannot figure out this error.
source to share