2012-03-11 261 views
0

Beeing新與facebook的事情。如何在登錄後獲取用戶信息? 下面是代碼: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> 

感謝您的幫助。和抱歉,如果我看傻了

回答

1

一旦你初始化的Facebook SDK(使用FB.init),你可以開始使用它的請求,事件等

documentation談到5 驗證方法。你需要哪一個取決於你想要做什麼,如果用戶已經授權的應用程序和權限,您要使用。

如果用戶沒有在您的應用程序登錄,您需要使用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方法,在我給你的例子。

+0

感謝您的幫助。 – 2012-03-13 20:43:27

0

對於初學者非常:我不得不把一段代碼尼贊給了我和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>