簡體   English   中英

JavaScript-顯示用戶是否登錄到Facebook

[英]javascript - show if user logged to facebook

我想檢查用戶是否登錄了Facebook(不一定從我的網站登錄)。 當我調試腳本時,我看到它無法執行FB.getLoginStatus()。 我也嘗試了其他FB函數,但是似乎除了FB.init之外,其他函數均無法識別。 他們為什么失敗? 任何幫助,將不勝感激

<div id="fb-root"></div>
<script type="text/javascript" src="//connect.facebook.net/en_US/all.js">
</script>    
<script type="text/javascript">   
           window.fbAsyncInit = function() {
              FB.init({
                appId      : 'my_app_id', // App ID
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
              });
            };

            // 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));
        function GetFBLoginStatus() {
        debugger;
        window.fbAsyncInit()
                alert("get status");
        FB.getLoginStatus(function(response) {
            alert("inside call back function"); 
            if (response.status === 'connected') {
              //  var uid = response.authResponse.userID;
               // var accessToken = response.authResponse.accessToken; 
             //   alert("Connected FBID = " + uid);
            } else if (response.status === 'not_authorized') { 
                alert("not_authorized"); 
            } else {
                alert("Not Logged into facebook");
            }
            return true;
        }, true)
    } 
    </script>

編輯

您似乎在代碼中省略了一些重要的內容,請嘗試使用此功能,它對我有用:

window.fbAsyncInit = function() {
  FB.init({
    appId      : '', // App ID
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  // Here we subscribe to the auth.authResponseChange JavaScript event
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // logged into the app
    alert('logged');
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app
    alert('not authorized but logged into fb')
    } else {
      // not logged into Facebook
    alert('not logged');
    }
  });
  };
  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));

最后一件事:幾個小時內注意“重復問題”

結束編輯

那是你的代碼嗎? 這個“ my_app_id”

 appId      : 'my_app_id', // App ID

這是fb在創建應用程序時將生成的值,您需要在所有操作之前使用此數字。

如果您不是網絡開發人員(需要創建應用程序),則可以轉到此處 ,加入開發人員團隊后,您可以創建一個應用程序並獲取您的應用程序ID。 足夠清楚了嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM