[英]Facebook API: checking if user is logged in on connect
我一直试图让我的网站检查用户是否在页面加载时登录,但是尽管遵循API指令我仍然无法使其工作,这是一个例子:
<!DOCTYPE html>
<html>
<head>
<title>My Facebook Login Page</title>
</head>
<body>
<div id="fb-root"></div>
<script>
alert('hello');
window.fbAsyncInit = function() {
FB.init({
appId : 'APP_ID',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
FB.getLoginStatus(function( response ) {
console.log(response);
alert('hello2');
});
FB.Event.subscribe('auth.statusChange', function(response) {
alert('hello2');
});
FB.Event.unsubscribe('auth.statusChange');
};
(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>
</body>
</html>
除非用户已登录,否则警报永远不会被命中,如果他已注销,则此警报或该代码块内的任何内容都不会触发。
在上面的例子中删除尾随的逗号(oauth:true,)//可能只是来自复制/粘贴,但想要消除任何陷阱
对于调试,控制台日志记录是我的首选方法,但由于您甚至没有看到警报,因此您的情况有点棘手。
FB.Event.subscribe("auth.statusChange", function(response) {
console.log(response);
});
FB.Event.unsubscribe("auth.statusChange");
// If you were to get subscribe to work, think about unsubscribing at some point to, since most likely that's only for an initial check
或者,尝试使用getLoginStatus而不是事件订阅。
FB.getLoginStatus(function( response ) {
console.log(response);
});
响应应该为您提供一个对象,您可以检查response.status
及其可能的值是:connected,not_authorized或unknown response.authResponse
将包含以下属性:accessToken,expiresIn等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.