Reputation: 1500
I've been using CodeIgniter for a couple of years. I don't make things too complex, basically, I do this in my controller's constructor:
$conf = array ( 'appId' => 'MY APP ID', 'secret' => 'MY SECRET KEY');
$facebook = $this->load->library('facebook/facebook',$conf);
And then, to log the user in, I use the same method on the example.php provided by facebook (get user ID, try catch making an API call). Everything on my controller's constructor.
In the couple of years of using this methodology, everything has worked ok, until now. Last week I coded an app writing basically the same code in every app I've done in the past, the problem is I'm getting A LOT of oAuth Access Token errors, to be precise, I'm getting a message saying that the access token has expired and I need a new one to query the facebook API.
So, I know I'm not giving much information about my code, but what I like to discuss is the methodology and not the code itself:
How do you handle facebook login in your app? Do you think the way I'm doing it is OK or I have to do it in a method and not in the constructor? Do I have to save the access token in the database so I can use the same access token when the user come back to the app?
It would be great if you have an example with codeigniter.
Thank you very much in advance!
P.s: I'm autoloading CI sessions library, using the channel.php file with an expire date of a year and using p3p headers so browsers do accept cookies. I really don't know what I'm missing. P.s2:"I've read a lot of related questions here in stackoverflow, I've tried some advices I've read here, but I get the same result.
Upvotes: 1
Views: 1548
Reputation: 162
Version 3.2.1 of the SDK contains a bug, which is fixed in the (yet untagged) 3.2.2 release. Replace your base_facebook.php file with the latest version on github at:
https://raw.github.com/facebook/facebook-php-sdk/master/src/base_facebook.php
This commit:
https://github.com/facebook/facebook-php-sdk/commit/ca9472b3312dab3fdcfbffb4e45eb091f582dcb7
references the fix of "trying to reuse spent auth tokens".
This drove me insane. It may well be that there was a loophole closed in the graph API that requires this updated code and there was never a bug... either way, it's fixed in the latest.
My codeigniter code:
$config['appId'] = '408995679168867';
$config['secret'] = 'secret';
$this->load->library('facebook', $config);
and then
$fbid = $this->facebook->getUser();
$this->fb_me = $this->facebook->api("/me");
should have the try/catch in there and etc, but i left it out for readibility.
Upvotes: 2