I try to integrate FB functionality in my Symfony2 app.
I have a base.html.twig where I initialise the javascript facebook api:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '{{ fb_appid }}',
xfbml : false,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/de_DE/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Then, in my DefaultController.php I want to retrieve the session and after that the current logged in users facebook id:
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookJavaScriptLoginHelper;
/**
* @Route("/overview", name="_overview")
* @Template()
*/
public function overviewAction()
{
$helper = new FacebookJavaScriptLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
echo $ex."<br>";
} catch(\Exception $ex) {
// When validation fails or other local issues
echo $ex."<br>";
}
if ($session) {
// Logged in
echo "some";
}else{
echo "none";
}
}
And all I get is "none".
I dont know why I have no session, because the javascript method gives me the "connected" status.. so user is logged into facebook and have permitted my app.
Could anyone help me a little on this please, since this should not be such a difficult task?! Thanks in advance :)
Ok I've found out by myself: I had to configure cookie and status on Fb.init()
window.fbAsyncInit = function() {
FB.init({
appId : '{{ fb_appid }}',
xfbml : false,
version : 'v2.1',
cookie : true,
status : true
});