[英]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.