0

Facebook API 的 FB 对象在初始化后似乎为空。这是我正在使用的代码,非常简单。问题是它说我的“/me”响应是未定义的。我已登录,只是仔细检查了一遍,所以情况并非如此。我已经允许它使用我的数据。

为什么我的“/me”响应未定义?

另外,有没有办法可以移动我的代码:

function testAPI() {
                        console.log('Welcome!  Fetching your information.... ');
                        FB.api('/me', function(response) {
                            console.log('Good to see you, ' + response.name + '.');
                        });
                    }

                    testAPI();

放入一个单独的 .js 文件(将代码包装在一个函数中),并在 window.fbAsyncInit 中调用函数时加载它?因为我尝试过的每一种方式都导致了失败。

<!doctype html>  
<html lang="en">  
    <head>  
        <meta charset="utf-8">
        <title>FB-Stuff</title>
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                  appId      : '[my code]', // App ID
                  channelUrl : '//[my channel]', // Channel File
                  status     : true, // check login status
                  cookie     : true, // enable cookies to allow the server to access the session
                  xfbml      : true  // parse XFBML
                });

                function testAPI() {
                    console.log('Welcome!  Fetching your information.... ');
                    FB.api('/me', function(response) {
                        console.log('Good to see you, ' + response.name + '.');
                    });
                }

                testAPI();
            };

            // 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));
        </script>
    </body>
</html>

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ------

现在我根本没有收到任何函数调用/错误消息。这是新代码:

<!doctype html>  
<html lang="en">  
    <head>  
        <meta charset="utf-8">
        <title>FB-Stuff</title>
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="mycode.js"></script>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                  appId      : 'xxx', // App ID
                  channelUrl : '//xxx/channel.html', // Channel File
                  status     : true, // check login status
                  cookie     : true, // enable cookies to allow the server to access the session
                  xfbml      : true  // parse XFBML
                });

                FB.getLoginStatus(function(response) {
                  if (response.status === 'connected') {
                        facebookInit();
                    }
                });
            };

            // 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));
        </script>
    </body>
</html>

在 mycode.js 里面是:

function facebookInit(){
    function testAPI() {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.');
        });
    }
}
4

1 回答 1

1

您需要将调用推迟到FB.api解决状态之后- 为此使用FB.getLoginStatus或收听其中一个事件。

不确定移动代码是什么意思 - 只要在 JS SDK 之前加载此代码,您就可以将其放在任何地方。

于 2012-12-08T18:54:50.183 回答