<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'MY_APP_ID',
status : true,
cookie : true,
xfbml : true
});
FB.Event.subscribe('auth.login', function(response) {
$.ajax({
url: "http://example.com/fbconnect",
success: function(){
window.location.reload();
}
});
});
};
(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>
<div class="fb-login-button" data-perms="email,user_birthday,user_hometown,user_location">Login with Facebook</div>
它只适用于两种情况:
- 当用户未登录并登录 Facebook 时,就会触发该事件。
- 当用户登录但尚未授权该应用时,它会触发。
第三种不起作用的情况是当用户登录并且他已经授权该应用程序时,FB.Event.subscribe('auth.login')
不会触发! !
最佳答案
我遇到了和你一样的事情,我发现了一个函数“getLoginStatus”,你可能会感兴趣。
当用户已经登录并授予您的应用所有权限时,无需再次登录,因此不会触发该事件。
但是,如果“getLoginStatus”的响应是“已连接”,则当用户已经登录并授予您的应用程序所有权限时,您可以执行您想做的操作。
完整的脚本可能如下所示:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'APPID', // App ID
channelUrl : 'CHANNEL_URL', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me', function(response) {
if (response.name != undefined ) {
console.log("Welcome, " + response.name);
}
});
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but not connected to the app
} else {
// the user isn't even logged in to Facebook.
}
});
// Additional initialization code here
};
// 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>
关于javascript - FB.Event.subscribe ('auth.login' )不会每次都会触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979469/