PHP-SDK for Facebook Connect Generating Error on Logout
I copied the code from the example in the sdk, and made a few small changes, (and the following error occurs even without the changes) and the s开发者_运维技巧cript logs me in correctly.
It recognizes that I'm connected, produces the appropriate session information, etc. However, the logout link facebook.com/logout.php?next=http://www.stockyardmagazine.com/fb/&access_token=xxyy sends me to the aforementioned page and throws an error. "Sorry, an error has occurred. We're working on getting this fixed as soon as we can." Also, I remain logged in.
Is this a problem in my code, or something on FB's end? I'm sandboxing in http://www.stockyardmagazine.com/fb/. This is the whole page:
<?php
require 'facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'numberxxyy',
'secret' => 'abcdef123',
'cookie' => true,
));
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>facebook</title>
<meta name="robots" content="NOINDEX"></meta>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
<h2>With actual permissions</h2>
<fb:login-button size="medium" perms="email,user_birthday,user_location,user_interests,user_education_history" onlogin="Log.info('onlogin callback')">Connect via Facebook</fb:login-button>
<h2>For those uncomfortable, and just want to test</h2>
<fb:login-button size="medium" onlogin="Log.info('onlogin callback')">Connect via Facebook</fb:login-button>
</div>
<?php endif ?>
<h3>Session</h3>
<?php if ($me): ?>
<pre><?php print_r($session); ?></pre>
<h3>User</h3>
<img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
<?php echo $me['name']; ?>
<h3>User Object</h3>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
I provided a connect button with fewer permissions for testing, if that matters to some of you.
Thanks!!
You need to have the fb-root
DIV tag first, then call FB.init()
and then FB.logout()
There is a fb-root
div on line 54 of the original.
The edited javascript is below; now the page just refreshes constantly.
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : '<?php echo json_encode($session); ?>', // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.logout(function(response) {
// user is now logged out
});
FB.login(function(response) {
// user is now logged out
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
Sarfraz is correct, but you also have another issue. Follow your logic here:
FB.logout(function(response) {
// user is now logged out
});
FB.login(function(response) {
// user is now logged out
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
You log the user out, then you log the user in, then you subscribe to a login event which reloads the page. Once the page reloads, you're logging them out again, in again, refresh, ad nauseum.
Remember these are asynchronous calls. Any of them can return first. If you really want this logic, then you should write it something like this:
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : '<?php echo json_encode($session); ?>', // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.logout(function(response) {
// user is now logged out
FB.login(function(response) {
// user is now logged out
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
});
});
};
But still, I don't see the point...
精彩评论