Facebook会话没有存储?

时间:2011-08-09 18:44:20

标签: facebook session

一旦我启动了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>

1 个答案:

答案 0 :(得分:0)

要从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 SDK(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>