I am having trouble to authenticate users through the Facebook PHP SDK.
Basically I want to show a “log in with facebook” button if the user is not already authenticated. Otherwise, present the user name. Here is the code:
require_once 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
'cookie' => true
));
$userId = $facebook->getUser();
var_dump($userId);
if ($userId) {
$userInfo = $facebook->api('/' . $userId);
echo "Welcome, {$userInfo['name']}";
} else {
echo '<a href="' . $facebook->getLoginUrl() . '">' .
'<img src="images/icon_facebook_login.png" alt="Login with Facebook" /></a>';
}
The trouble is I always get 0 from $facebook->getUser(). I looked at the code in base_facebook.php and while processing the function a request param signed_request is expected in the return URL. The only params I see in the URL Facebook returns are code and state.
Any ideas what I might have messed up? Thanks in advance.
EDIT: As suggested by ogi I made a hand-made request with the code param to graph.facebook.com/oauth/access_token and I did receive access_token. Now, the only question remaining is how to accomplish this through the PHP SDK?
The problem was I didn’t copy
fb_ca_chain_bundle.crtfile to the directory wherefacebook.phpandbase_facebook.phpreside. IngetAccessTokenFromCode()the code tofacebook.com/oauth/access_token...fails but since they capture the exception, it is very hard to figure out.Hopefully this will save someone some hours of code tracing.