Facebook login: getUser() always return 0
I have a problem regarding login with facebook api for website.
Previously I made 1 facebook app (complete with the required
configuration). I made 1 php code (test_login_fb.php for login page). I've
tested it (domain: let's just say domain1.com), and it works smoothly.
Then I tried to do the same thing for another domain. I copied the file
and use it (domain: let's just say domain2.com), but it seems that the
login function does not work. It always show the login button which means
the getUser() always return 0.
I'm sure I've changed the app_id and secret_key from the code perspective,
also the url and other configuration on the facebook side.
Can anyone help me?
Here is my code
test_login_fb.php
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('./facebookconnect/facebook.php');
$config = array(
'appId' => 'xxx',
'secret' => 'xxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
echo '<br>'.$facebook_user_id = $facebook->getUser();
echo '<br><img
src="https://graph.facebook.com/'.$facebook_user_id.'/picture"
/>';
echo '<br>Email: ' . $user_profile['email'];
$logoutUrl = $facebook->getLogoutUrl(array('next' =>
"http://www.domain2.com/test_logout_fb.php"));
echo '<br/><a href="'.$logoutUrl.'">Logout</a>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$perms = array('scope' => 'email');
$login_url = $facebook->getLoginUrl($perms);
//$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
</body>
</html>
Thank you kindly.
No comments:
Post a Comment