[英]PHP-SDK for Facebook Connect Generating Error on Logout
我将示例中的代码复制到sdk中,并进行了一些小的更改(即使不进行更改,也会发生以下错误),并且脚本正确地登录了我。
它识别出我已连接,产生了适当的会话信息等。但是,注销链接facebook.com/logout.php?next= http://www.stockyardmagazine.com/fb/&access_token=xxyy将我发送到前述页面并引发错误。 “很抱歉,发生了错误。我们正在努力尽快解决此问题。” 此外,我仍保持登录状态。
这是我的代码中的问题,还是FB方面的问题? 我正在http://www.stockyardmagazine.com/fb/中进行沙箱测试。 这是整个页面:
<?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>
我提供了一个连接按钮,该按钮具有较少的测试权限(如果对某些人来说很重要)。
谢谢!!
您首先需要具有fb-root
DIV标记,然后调用FB.init()
和FB.logout()
原始文件的第54行有一个fb-root
div。
编辑的javascript如下; 现在页面不断刷新。
<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是正确的,但是您还有另一个问题。 在这里遵循您的逻辑:
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();
});
您注销用户,然后登录用户,然后订阅登录事件,该事件将重新加载页面。 页面重新加载后,您将再次注销它们,然后重新刷新广告。
请记住,这些是异步调用。 他们中的任何一个都可以先返回。 如果您确实想要这种逻辑,那么应该这样写:
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();
});
});
});
};
但我仍然不明白要点...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.