Matthieu Granger
Matthieu Granger

Reputation: 41

Fail to retrieve facebook cookie with SDK v5

I am trying to migrate from facebook php sdk v4 to v5. I am following the steps of facebook developper's support. Cookies are enabled:

<body id="home" >

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
    FB.init({
      appId  : 'xxx',
      cookie: true, // This is important, it's not enabled by default
      version: 'v2.2'
    });
  };

  (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/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk')); 
</script>

I can see that the cookie "fbsr_appid" is set in firefox console.

When I call (in my ajax request) the php function getJavaScriptHelper, I do not retrieve the access token. ($accessToken is not set)

$fb = new Facebook\Facebook([
  'app_id' => '{xxx}',
  'app_secret' => '{xxx}',
  'default_graph_version' => 'v2.2',
]);

$helper = $fb->getJavaScriptHelper();

try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

if (! isset($accessToken)) {
  echo 'No cookie set or no OAuth data could be obtained from cookie.';
  exit;
}

Here is the javascript that is triggered on click on login button:

logInWithFacebook = function() {
    FB.login(function(response) {
    if (response.authResponse) {
    var fbid = response.authResponse.userID;    
    //document.cookie = 'fb_token=' + FB.getAuthResponse().accessToken;
    $.ajax({
    type : 'GET', 
    url : 'ajx/traitementloginFB.php' , 
    dataType: "json",
    data: {'fbid':bid,'accesstoken':FB.getAuthResponse().accessToken},  
    beforeSend : function() { 
    },
    success : function(data){ 
    var libele = data[0].libele;
    var retour = data[0].retour;
    if (retour=="Nok") {
        $('#modalmsg3').html(libele);
    } else if (retour=="Ok" || retour=="Logged in"){
        window.location = 'home';               
    }           
    } ,
    error : function(e){
    }
});
  } else {
    alert('User cancelled login or did not fully authorize.');
  }
});
return false;
};

Is there a way to manually set the $accesstoken variable, or does someone know where my problem come from?

Upvotes: 3

Views: 698

Answers (1)

Matthieu Granger
Matthieu Granger

Reputation: 41

Ok, my bad. Find the answer the next morning: I had to suppress the brackets the $fb configuration

$fb = new Facebook\Facebook([
  'app_id' => ' **{** xxx **}** ',
  'app_secret' => ' **{** xxx **}** ',
  'default_graph_version' => 'v2.2',
]);

The rest of the code works.

Upvotes: 1

Related Questions