3

I am using the FB facebook-php-sdk-v4-5.0-dev.zip API files.

I'm attempting to make the Facebook javascript and php SDKs work together. I have FB.login working find, I can make Graph requests, etc. Now I want to pass off the access token to my PHP code, and continue from there. I am initializing FB in javascript:

window.fbAsyncInit = function() {
    FB.init({
        appId      : appId, //my appId
        cookie     : true,  // enable cookies for session access
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.2' // use version 2.2
    });

And now that FB in initialized, I do some other work using the API, after which I push out to my php code with:

window.location.replace(<my url>.php);

Inside my PHP application, I have the following:

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

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

if (isset($accessToken)) 
{
    print("Logged in already");
}
else
{
    print("Not logged in");
}

What I see is "Not logged in". From what I can tell, no cookie is generated (I dumped out $_COOKIE at the top of my PHP file). I have also monitored the network calls, and can see no cookie coming down from Facebook on my oauth requests... which is where I would assume that such a cookie would come from.

Thanks, Andy

1
  • Did you finally solve this ? I have the same problem. $jsHelper->getAccessToken() returns null, and I don't see any facebook related cookie. Commented Jul 16, 2016 at 9:08

2 Answers 2

3

You need to set cookie to true while initializing the javascript object. Otherwise FB Javascript Api won't store a cookie.

FB.init({
    appId   : '{app-id}',
    cookie  : true,
    version : 'v2.5'
});
Sign up to request clarification or add additional context in comments.

Comments

1

I'd recommend that you update to the v5.0.0 of the PHP SDK, or is there a specific reason you're using this outdated one?

Furthermore, have a look at the example at

Copied sample code:

# /js-login.php
$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  '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;
}

// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

$_SESSION['fb_access_token'] = (string) $accessToken;

// User is logged in!
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

2 Comments

Well, I thought that I had, and that their naming scheme was whack ("v4-5.0"). I will go back and see if I can find better. That sample code is pretty much (except for local variable names and formatting) the same as mine. But I'll look for a more up-to-date SDK.
Ok, I just followed your link to the PHP SDK page, and clicked the link to download the v5 source code, and got the exact same file (facebook-php-sdk-v4-5.0-dev). So that isn't the problem, and I compared line-by-line, and I have the same code as the sample. So I'm missing something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.