0

我有一个大问题,我试图解决好几天但无法解决。

我得到了以下代码来登录 Facebook 上的应用程序。一切都运行良好,除了当页面重新加载时(使用 window.location.reload();)它没有更新 API /me,所以它总是返回登录按钮。

例外:当我单击 F5 刷新页面时,它运行良好

如果有人可以提供帮助,将不胜感激

谢谢

 <?php

require 'facebook.php';

 // Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId'  => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxx',));

$user = $facebook->getUser();

 // login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
 } else {
 $loginUrl = $facebook->getLoginUrl();
 }

 $user = $facebook->getUser();
 // Session based API call.
if ($user) {
  try {
    $me = $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">
<head>
 <title>test full name and photo</title>
 </head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId      : '<?php echo $facebook->getAppId(); ?>', // App ID
        channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        oauth      : true, // enable OAuth 2.0
        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/fr_CA/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

 function fbLogin() {
        FB.login(function(response) {
      if (response.status == 'connected') {
                window.location.reload();            }
      else {
        alert('Try to connect');
        }
    }, {scope: 'email,user_likes'});
    }
  </script>




<?php if ($user): ?>
USER LOGGIN

<?php else: ?>
<div>
 Connexion button: <INPUT TYPE="BUTTON" ONCLICK="fbLogin()" value="login">
</div>
<?php endif ?>


<?php if ($user): ?>
    <?php $uid = $me['id']; ?>
    <div align="center">
        <img id="image" src="https://graph.facebook.com/<?php echo $uid; ?>/picture" />
        <div id="name"><?php echo $me['name']; ?></div>
        <div id="name">UID: <?php echo $me['id']; ?></div>
        <div id="name">Courriel <?php echo $me['email']; ?></div>

    </div>
<?php endif ?>
 </body>
</html>
4

1 回答 1

1

FB.login不返回,response.status是返回的FB.getLoginStatus。另请参阅FB.login文档以获取更多信息。

于 2013-01-19T09:43:31.773 回答