开发者

Facebook authentication with codeigniter (active access token must...)

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...


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜