我正在尝试整合Facebook PHP-SDK 4和Symfony2
我使用composer并使用Facebook开发人员文档中的一些基本代码安装了Facebook SDK
生成Facebook登录网址
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));
}
登录成功后回调或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());
}
首先我得到了错误
FacebookSDKException: Session not active, could not store state.
我遇到this link并且说会话有问题并且使用了
$helper->disableSessionStatusCheck();
由于此登录工作正常,但当我在FB登录后被重定向到Facebook回调URL /重定向URL时,我得到了异常
DefaultApplication is not set
所以我在FacebookSession::setDefaultApplication('appId', 'secret');
successAction
登录以下部分后仍然出现错误,我无法在此处获得会话。
try {
$session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
echo " When Facebook returns an error";
} 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();
这里导致错误的是什么,我的Facebook登录证书是正确的。
我无法弄清楚这个错误。
答案 0 :(得分:1)
尝试这样做。
$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
);