Symfony2 and PHP SDK 4.0 session issue while login

327 views Asked by At

I am trying to integrate Facebook PHP-SDK 4 and Symfony2

I installed Facebook SDK using composer and using some basic code from Facebook developer documentation

Generate Facebook 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 login success

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());
    }

First I got error

FacebookSDKException: Session not active, could not store state.

I came across this link and it says there is issue with session and used

$helper->disableSessionStatusCheck();

because of this login worked, but when I got redirected to Facebook callback URL / Redirect URL after FB login I got Exception that

DefaultApplication is not set

so I added FacebookSession::setDefaultApplication('appId', 'secret'); at successAction

still I get error after login at section below, I can't get 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 get error here catch (\Exception $ex) {
echo " When validation fails or other local issues"; }

$session = helper->getSessionFromRedirect();

what is causing error here, my Facebook login Credentials are correct.

I am unable to figure out this error.

1

There are 1 answers

0
Raaz On

Try doing it this way.

$helper = new FacebookRedirectLoginHelper(
                $this->generateUrl('app_home',[],true),//generates absolute ur;
                $this->container->getParameter('facebook_sdk.app_id'), //api id
                $this->container->getParameter('facebook_sdk.app_secret')//app secret
            );