33

我正在尝试检查用户是否已使用 facebook 登录并在 JS 控制台中收到该错误。我的代码如下所示:

<div id="fb-root"></div>
<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
        });
    };

    // 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));

    FB.getLoginStatus(function (response) {
        if (response.status === 'connected') {
            // the user is logged in and has authenticated your
            // app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed
            // request, and the time the access token
            // and signed request each expire
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook,
            // but has not authenticated your app
        } else {
            // the user isn't logged in to Facebook.
        }
    });
</script>

可能是什么问题,对于如何解决它有什么建议吗?

4

7 回答 7

55

在 Chrome 中,您会收到错误消息:Uncaught ReferenceError: FB is not defined

我喜欢在 Facebook 的初始化函数fbAsyncInit()中触发自定义事件。然后我不限于在 fbAsyncInit 函数中执行所有与 FB 相关的脚本。我可以把它放在任何地方,只需监听要触发的自定义事件。

window.fbAsyncInit = function() {
    FB.init({
        appId      : '123456789',
        status     : true,
        cookie     : true,
        xfbml      : true  
    });

    $(document).trigger('fbload');  //  <---- THIS RIGHT HERE TRIGGERS A CUSTOM EVENT CALLED 'fbload'
};

//MEANWHILE IN $(document).ready()
$(document).on(
    'fbload',  //  <---- HERE'S OUR CUSTOM EVENT BEING LISTENED FOR
    function(){
        //some code that requires the FB object
        //such as...
        FB.getLoginStatus(function(res){
            if( res.status == "connected" ){
                FB.api('/me', function(fbUser) {
                    console.log("Open the pod bay doors, " + fbUser.name + ".");
                });
            }
        });

    }
);
于 2013-04-12T21:34:46.623 回答
52

您在加载和/或初始化 SDK 之前调用 FB.getLoginStatus。等待它,这就是 fbAsyncInit 事件的用途。所以把方法调用放在那里。

于 2013-02-28T19:48:56.717 回答
10

我使用了以下简单的方法,它可以正常工作:

head我加载 SDK 的部分中:

<script type="text/javascript" src="//connect.facebook.net/en_US/sdk.js"></script>

然后在body您的实际内容所在的页面中,我使用了这个:

<script>

  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    if (response.status === 'connected') {
      testAPI();

    } else if (response.status === 'not_authorized') {
      FB.login(function(response) {
        statusChangeCallback2(response);
      }, {scope: 'public_profile,email'});

    } else {
      alert("not connected, not logged into facebook, we don't know");
    }
  }

  function statusChangeCallback2(response) {
    console.log('statusChangeCallback2');
    console.log(response);
    if (response.status === 'connected') {
      testAPI();

    } else if (response.status === 'not_authorized') {
      console.log('still not authorized!');

    } else {
      alert("not connected, not logged into facebook, we don't know");
    }
  }

  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }

  $(document).ready(function() {
    FB.init({
      appId      : '1119999988888898981989819891',
      xfbml      : true,
      version    : 'v2.2'
    });
    checkLoginState();
  });
</script>
于 2015-03-13T14:36:15.017 回答
3

只需在标签中直接使用带有 facbook sdk 的脚本

    <script type="text/javascript" src="//connect.facebook.net/en_US/all.js#xfbml=1&appId=YOUR_APP_ID" id="facebook-jssdk"></script>
于 2017-10-13T14:22:29.693 回答
1

我们面临同样的问题,并且由于 FB sdk 没有加载(或加载延迟),这在某些情况下会导致问题(或破坏 javascript),这取决于它。

为了解决这个问题,我们委托了所有与 facebook 相关的调用。我们创建了一个类似的函数:

function callOnFBSDKLoad(callback){
    if (window.isFBInitialized) {
        callback();
    }
    else {
        jQuery(document).bind('fbInitialized', callback);
    }
}

自定义事件在哪里fbInitialized可以在此处了解有关自定义事件的更多信息 并且window.isFBInitialized是加载 SDK 时设置的变量。

callback 是封装 FB 相关事件的方法。例如

var FBLogin = function(){
   FB.login(...);
}

callOnFBSDKLoad(FBLogin)

PS:请忽略语法和命名约定,我基本上是在分享解决此类问题的想法。

于 2015-12-15T07:50:26.223 回答
0

一个更简单和可靠的解决方案。SDK 加载时调用函数。我会命名它triggerLoginCheck()

<script>
  window.fbAsyncInit = function() {
    FB.init({
      //TO DO: Add your unique Facebook app ID (All numbers).
      appId      : '***unique_app_id***',
      cookie     : true,
      xfbml      : true,
      version    : 'v2.8'
    });
    //Call the function here
    triggerLoginCheck();

    FB.AppEvents.logPageView();   
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

稍后,在任何地方,放置这个:

function triggerLoginCheck() {       
       FB.getLoginStatus(function(response) {
       statusChangeCallback(response);
       console.log(response);  
       });
}
于 2017-06-24T18:52:02.423 回答
0

这对我来说很好。必须包括对象“Log.info.bind”。

var Log = { info: { bind: function(){ alert('Hi'); 测试API(); } } }
于 2018-09-05T21:13:51.950 回答