一旦我启动了FB.login(),我需要php代码来获取登录后的所有信息。在我更新到新的api之前,它工作得很好。有什么想法吗?会话似乎没有被存储?
window.fbAsyncInit = function() {
FB.init({
appId: 'xx',
status: true,
cookie: true,
xfbml: true,
oauth : true // enables OAuth 2.0
});
};
function test () {
window.location.reload();
} 下面是我的html:
<a href='#' onclick="FB.login(test, {scope: 'user_birthday,user_photos,user_website,user_status,email,user_location,user_about_me,sms,offline_access,read_stream,publish_stream,user_photo_video_tags'});">login plz</a>发布于 2011-08-10 22:37:39
要从FB.login()获取数据,您可以订阅auth.login事件,例如:
FB.Event.subscribe('auth.login', function(response) {
console.log(response);
console.log(response.session.access_token);
console.log(response.session.expires);
console.log(response.session.secret);
console.log(response.session.session_key);
console.log(response.session.sig);
console.log(response.session.uid);
});您应该考虑使用最新的PHP (v3.1.1)和JavaScript SDK来维护客户端和服务器之间的会话:http://developers.facebook.com/blog/post/534/
以上博客文章中的代码示例:
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($user_profile) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print_r($user_profile, true)) ?>
</pre>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>https://stackoverflow.com/questions/7001261
复制相似问题