Facebook登录注销实施

时间:2012-03-11 09:53:25

标签: facebook events login

Beeing与facebook的新事物。登录后如何获取用户信息? 这是代码:

 <html>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'APP_ID',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true,
      });
    };
    (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>
  <p><fb:login-button autologoutlink="true"></fb:login-button></p>
</body>

我红了,我必须订阅auth.login事件,然后使用FB.api ...很棒,但你怎么做?

我尝试了以下内容:

 <html>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'APP_ID',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true,
      });
    };
  FB.auth.login{
        FB.api('/me', function(response) {
          alert(response.name);
        });
      }
    };

    (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>
  <p><fb:login-button autologoutlink="true"></fb:login-button></p>
</body>

感谢您的帮助。抱歉,如果我看起来很蠢......

2 个答案:

答案 0 :(得分:1)

初始化facebook sdk后(使用 FB.init ),您可以开始将其用于请求,事件等。

documentation讨论了5 Auth Methods 。您需要哪一个取决于您想要做什么,以及用户是否已经授权您的应用程序以及您将要使用的权限。

如果用户未登录您的应用程序,则需要使用login方法:

FB.login(function(response) {
    if (response.authResponse) {
        // logged in
    }
    else {
        // not logged in
    }   
});

如果他已经登录,或者您不知道他的状态是什么getLoginStatus

FB.getLoginStatus(function(response) {
    if (response.status === "connected") {
        // the user is logged in and has authenticated your app
        FB.api("/me", function(apiresponse) {
            console.log("api result: ", apiresponse);
        });
    }
    else if (response.status === "not_authorized") {
        // the user is logged in to Facebook, but has not authenticated your app
    }
    else {
        // the user isn't logged in to Facebook.
    }
});

要发出api请求,请使用我给你的示例中的api方法。

答案 1 :(得分:0)

对于初学者:我必须把Nizan给我的一段代码放在FB.init调用之后添加它:

 <html>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>
 <div id="fb-root"></div>
 <script>
   window.fbAsyncInit = function() {
  FB.init({
    appId      : 'APP_ID',
    status     : true, 
    cookie     : true,
    xfbml      : true,
    oauth      : true,
  });
  FB.getLoginStatus(function(response) {
if (response.status === "connected") {
    // the user is logged in and has authenticated your app
    alert("You are logged in");
}
else if (response.status === "not_authorized") {
    // the user is logged in to Facebook, but has not authenticated your app
}
else {
    alert("You are not logged");
}
});
};
(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>
 <p><fb:login-button autologoutlink="true"></fb:login-button></p>
</body>
</html>
相关问题