简体   繁体   English

登录时出现Symfony2和PHP SDK 4.0会话问题

[英]Symfony2 and PHP SDK 4.0 session issue while login

I am trying to integrate Facebook PHP-SDK 4 and Symfony2 我正在尝试集成Facebook PHP-SDK 4和Symfony2

I installed Facebook SDK using composer and using some basic code from Facebook developer documentation 我使用composer并使用Facebook开发人员文档中的一些基本代码安装了Facebook SDK

Generate Facebook Login Url 生成Facebook登录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 登录成功后回调或Facebook重定向

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 因为这个登录工作,但当我在FB登录后被重定向到Facebook回调URL /重定向URL我得到了Exception

DefaultApplication is not set

so I added FacebookSession::setDefaultApplication('appId', 'secret'); 所以我添加了FacebookSession::setDefaultApplication('appId', 'secret'); at successAction 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"; }
我在这里得到错误 catch (\\Exception $ex) {
echo " When validation fails or other local issues"; }
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. 这里导致错误的是什么,我的Facebook登录证书是正确的。

I am unable to figure out this error. 我无法弄清楚这个错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM