2012-05-01 70 views
3

我一直在試圖讓我的網站檢查用戶是否在頁面加載中登錄,但是我仍然無法使其工作,儘管遵循以下API說明,這裏是一個例子:Facebook 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> 

警報永遠不會連打,除非用戶登錄,如果他被註銷,此警報或任何將是代碼塊永遠不會觸發內。

回答

0

在你上面的例子刪除尾隨逗號(OAuth的:真)//這可能僅僅是從複製/粘貼,但要消除任何陷阱

對於調試,控制檯日誌是我的首選方法,但因爲你甚至沒有看到警報,你的情況有點棘手。

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和其可能的值是:連接,NOT_AUTHORIZED或未知 response.authResponse將包括像屬性:的accessToken,expiresIn等

+0

有時auth.statusChange事件不會觸發,我認爲這是由於網絡延遲或服務器超時。我發現與FB一起工作需要靈活處理頁面加載,而不是100%的時間「運行」。刷新可能會正確觸發事件。 –

+0

好吧,我剛剛意識到,它只適用於如果用戶登錄到Facebook(與應用程序authed或W/O應用程序authed) 有無論如何檢查用戶是否註銷或註銷用戶的狀態?我覺得getLoginStatus應該檢查用戶是否註銷,但不是,只有當他們授權了應用程序。 我採納了你對getLoginStatus的建議,但它似乎仍然不起作用。 – Ven

+0

嗯..你的迴應應該返回一個像這樣的對象:{authResponse:null, status:「unknown」} –

相關問題