0

I sign in with FB fine if I directly create a page at domain.com/someurl.php The moment I copy the same code (inc html) into a view file with codeigniter and go to domain.com/login/someurl.html it doesn't work. By it doesn't work I mean, I get an exception if i try to access /me after signing into facebook.

The message reads "An active access token must be used to query information about the current user"... In my controller i have my index function and all it does is load the view with the facebook login code.

I thought I may be doing something wrong but I copied the sample.php from the facebook sdk and paste it into the view file and the same thing happens. If i copy the sample code into the file at someurl.php it then works as expected...given the situation I suspected it may be something to do with codeigniter and possibly some config option I have that causes that behaviour...

I've been looking up the error for a while now and have found a few resources: http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens/

Facebook access_token invalid?

Facebook authentication issue

https://github.com/facebook/php-sdk/wiki/AccessToken but none of the suggested fixes have worked.

Right from the sample:

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'APPID',
  'secret' => 'SHHHH',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

It doesn't work in the view and I'm stumped as to why or why none of the fixes or "precautions" such as not using type or ensuring the redirect url is the same etc, work.

EDIT: I'm trying to avoid using domain.com/somefile.php and instead use a view file like every other page...

Cœur
  • 37,241
  • 25
  • 195
  • 267
zcourts
  • 4,863
  • 6
  • 49
  • 74
  • Views is **only** for presentation. – zerkms Jun 02 '11 at 23:53
  • Well yeah I'm not doing anything except rendering the "view" in that file but I need to determine if I show the log in or logout url right? but that's a far cry from what the problem may be – zcourts Jun 03 '11 at 00:30
  • @robinsonc494: it is far indeed, but you emphasized "It doesn't work in the view". Does it mean that it does work somewhere else? If no - why do we need to know about view at all? Anyway, it is not enough information. `Doesn't work` is too vague issue explanation. – zerkms Jun 03 '11 at 00:40
  • I'm not sure what other information I can give to make what I'm describing any clearer... okay the "it doesn't work in the view" is misleading as you've pointed out and no it doesn't work elsewhere when integrated with codeigniter. It works fine in a file that is not loaded by codeigniter (as I explained). So if I put the code in the controller it wouldn't make a difference it still wouldn't work. – zcourts Jun 03 '11 at 00:55
  • @robinsonc494: start from the scratch without codeignitEr at all then. Write **working** legacy script. After that move with attention all the working code to CI. – zerkms Jun 03 '11 at 00:57
  • I did as you suggested, it didn't work at all. – zcourts Jun 03 '11 at 09:08
  • @robinsonc494: I'm too tired of guessing. Give us the legacy script that doesn't work. And try to launch example shipped with php sdk. – zerkms Jun 03 '11 at 10:04

1 Answers1

0

The issue has cropped up in other places such as the Elliot Houghin fb/CI library. I found a solution documented in a tutorial here:

http://www.dannyherran.com/2011/02/facebook-php-sdk-and-codeigniter-for-basic-user-authentication/

The problem could be cause from one of several issues pointed out on a an issue on Elliot's github repo. I tried many of the suggested "fixes" but the one that seemed to help was not using the facebook instance as a class property. After trying the tutorial at the above link I tried a fresh install of CI again and created a facebook instance inside the function, the API calls with the FB SDK then worked and had no problems so far. Bug report with suggested fixes @

https://github.com/elliothaughin/codeigniter-facebook/issues/5

zcourts
  • 4,863
  • 6
  • 49
  • 74