Facebook要求用户登录,即使他们已经登录

时间:2011-11-14 04:48:34

标签: facebook

我很难按照http://developers.facebook.com/docs/reference/javascript/

中的说明让Facebook JS API正常工作

具体来说,即使用户已登录Facebook,也会要求用户登录。为此,我正在做以下事情:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '3120012412', // App ID goes here
      channelURL : '//WWW.qy.ORG/channel.php', // 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
    });

    // Additional initialization code here
    FB.login(function(response) {
    if (response.authResponse) {
       alert('Welcome!  Fetching your information.... ');
       FB.api('/me', function(response) {
       alert('Good to see you, ' + response.name + '.');
       alert('response' + response);

       FB.logout(function(response) {
         alert('Logged out.');
       });
     });
    } else {
      alert('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'email'});
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

我也尝试过:

FB.getLoginStatus(function(response) {
   if (response.authResponse) {
     // logged in and connected user, someone you know
   } else {
     // no user session available, someone you dont know
   }
});

即使我已经登录Facebook,这也会失败。

如果我希望用户完成最小的麻烦,只需基本访问公共信息,我该怎么办?我知道他们必须授权基本访问权限,但不应该反复询问它们吗?

1 个答案:

答案 0 :(得分:0)

您的第一个代码块正在记录用户,然后立即再次将其记录下来 - 因此,如果您已经运行了几次,那么您可能还没有登录。移除FB.logout块并再次尝试以查看它是否有所作为。

要记住的另一件事(可能是你的第二个例子不起作用的原因)是虽然你登录到facebook.com,但你必须通过FB.login登录到你自己的域名。 getLoginStatus将检查您的fbsr_ Cookie并认为您已登录,但是您必须已经拥有Cookie,而不是先调用FB.login

所以我建议使用它:

    // Additional initialization code here
    FB.login(function(response) {
       if (response.authResponse) {
          alert('Welcome!  Fetching your information.... ');
          FB.api('/me', function(response) {
             alert('Good to see you, ' + response.name + '.');
             alert('response' + response);
          });
       } else {
          alert('User cancelled login or did not fully authorize.');
       }
    }, {scope: 'email'});