我一直在努力Facebook Login
与我的网站集成,到目前为止,我大部分时间都取得了成功;但是,我仍然有一些疑问。
在我的主页上;我有一个按钮a href
,它将触发一个 Javascript 函数,该函数将通过弹出窗口提示用户登录 Facebook;如果是第一次,将提示用户另一个要求权限的弹出窗口!
用户登录后(并接受许可);(s) 他将被重定向到另一个页面,使用document.location.href
; 到目前为止,一切正常。然而,我接下来想做的是:
我想 - 在加载用户被重定向到的页面时 - 显示欢迎消息(欢迎 [电子邮件]) - 然后我想根据附加到的一些数据库信息动态(动态)生成一些元素用户名。
我正在尝试在 Javascript 中做到这一点:
<script>
$('document').ready(function(){
window.fbAsyncInit = function() {
FB.init({
appId : '[my app id]',
oauth : true,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
$(document).trigger('fbload');
};
$(document).on('fbload', function(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/me', function(response) {
user_email = response.email; //get user email
// Here, I can display the welcome message :)
});
} 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.
}
});
});
});
</script>
这段代码工作得很好:) - 但是,如果我FB.getLoginStatus
从函数中删除,这将导致以下代码:
$(document).on('fbload', function(){
FB.api('/me', function(response) {
console.log(response);
user_email = response.email; //get user email
console.log(user_email);
// Here, I can display the welcome message :)
});
我得到以下输出user_email
:
undefined
我似乎无法理解其中的逻辑!因为FB.getLoginStatus
只检查用户是否登录;为什么它(或缺少它)会破坏函数调用FB.api
?
如果有人能解释这里实际发生的事情的逻辑,我将不胜感激!
注意:无论如何我可能最终都会使用FB.getLoginStatus
,但我主要关心正在发生的事情的逻辑!