2013-08-03 26 views
11

我是使用Facebook SDK開發網站的新手。所以請耐心等待。getLoginStatus總是返回not_authorized

下面是我驗證我登錄Facebook的簡單代碼。 由於某種原因,我總是得到'not_autherized'的迴應,儘管我是我的應用程序的唯一開發者。我提供的應用號碼是正確的。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    </head> 
    <body> 
    <div id="fb-root"></div> 
    <script> 
     window.fbAsyncInit = function() { 
      // init the FB JS SDK 
      FB.init({ 
       appId  : 'censored app id', // App ID from the app dashboard 
       status  : true,    // Check Facebook Login status 
       cookies : true, 
       xfbml  : true    // Look for social plugins on the page 
      }); 

      FB.getLoginStatus(checkLoginStatus); 
      function checkLoginStatus(response) { 
       if (response && response == 'connected') { 
        alert('User is authorized!'); 
       } else { 
        alert('User not authorized!!!'); 
       } 
      }; 

      // Additional initialization code such as adding Event Listeners goes here 
     }; 

     // Load the SDK asynchronously 
     (function(d, s, id) { 
      if (d.getElementById(id)) return; 

      var js = d.createElement(s); js.id = id; 
      js.src = "//connect.facebook.net/en_US/all.js"; 

      var fjs = d.getElementsByTagName(s)[0]; 
      fjs.parentNode.insertBefore(js, fjs); 
     }(document, 'script', 'facebook-jssdk')); 
    </script> 
    </body> 
</html> 

有什麼我忽略了嗎?

非常感謝:)

回答

12

一位朋友幫我解決了這個問題。他非常有幫助地指出了Facebook忽視的文件。 當您創建一個新的Facebook應用程序時,它不會自動授權管理員和開發人員。授權應用程序是存在於「帳戶設置 - >應用程序」列表中的應用程序。如果它不在這個列表中,那麼它不被授權。

這意味着您需要在代碼中的某個時刻調用FB.login()以彈出用戶授權窗口。

請注意,這應該從一個按鈕中調用。否則,彈出窗口可能被阻止。

希望這可以幫助別人,但我。

乾杯:)