Graph API: Trouble Getting a Facebook Session

I seem to have inherited a Facebook app (on a website) that needs to be “upgraded” to Graph API and the latest SDK. Unfortunately, the documentation on the Graph API (as many seem to have noted) is very confusing. It assumes too much knowledge, for one thing. The immediate problem I’m having is establishing a Facebook session. Here is the “old” code that worked until recently:

<?php
include_once("inc/facebook_sdk/facebook.php");
$appId = '123456789098765';
$appSecret = '1a2b3c4d5e6f7g8h9i0j';
$return_url = 'http://domain.com/FBsetup.php';
$homeurl = 'http://domain.com/FBsetup.php';
$fbPermissions = 'publish_actions,publish_stream,manage_pages,status_update';
$facebook = new Facebook(array(
  'appId'  => $appId,
  'secret' => $appSecret,
  'cookie' => true
));
$fbuser = $facebook->getUser();
?>

Here is some “new” code that I more or less lifted from various tutorials:

<?php
define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook_sdk_test/facebook-php-sdk-v4-4.0-dev/src/Facebook/');
require_once __DIR__ . '/path/to/facebook-php-sdk-v4-4.0-dev/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
$api_key = '1234567890987654';
$api_secret = '1a2b3c4d5e6f7g8h9i0j';
$redirect_login_url = 'https://domain.com/accounthome.php';
FacebookSession::setDefaultApplication($api_key, $api_secret);
$helper=new FacebookRedirectLoginHelper($redirect_login_url);
$session = $helper->getSessionFromRedirect();
if($session) {
  try {
    $user_profile = (new FacebookRequest(
      $session, 'GET', '/me'
    ))->execute()->getGraphObject(GraphUser::className());
    echo "User: " . $user_profile->getUser();
  } catch(FacebookRequestException $e) {
    echo "Exception occurred, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
  }   
}
?>

Yes, I changed some of the information to protect the guilty. :wink:

Anyway, as far as I can tell, everything is fine until it gets to this: $session = $helper->getSessionFromRedirect(); The session variable remains empty. Truthfully, I’m not sure why the redirect is involved and why the session has to be retrieved from it. How do I successfully establish a Facebook session in the new environment?

Can anyone help? If I get this immediate issue resolved, hopefully I can figure out the rest, but I can’t promise I won’t have follow-up questions. :smile:

Thanks!

You may get a better response if you had posted this over in php forms?

I wish you luck.

It can be moved - we have the power.

I’ll move it to PHP for you, @Cubbie_Blue, and hopefully somebody there will be able to assist.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.