javascript 在 FB.init() 之前调用 Facebook 登录错误 FB.login()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16498641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:55:38  来源:igfitidea点击:

facebook login error FB.login() called before FB.init()

javascriptfacebook

提问by Glenn Derycke

i have a problem with loggin in into facebook i keep getting the error code: FB.login() called before FB.init(). i done various things but keep getting the error. here's the html code:

我在登录 facebook 时遇到问题,我不断收到错误代码:FB.login() 在 FB.init() 之前调用。我做了各种各样的事情,但不断收到错误消息。这是html代码:

 <!-- Javascript
        =================================================== -->
        <script src="//connect.facebook.net/en_US/all.js"></script>
        <script src="js/facebook.js"></script>
    </head>

    <body>
        <div id="fb-root"></div>
        <script>
            var email;
            var name;
            window.fbAsyncInit = function() 
            {
                FB.init({
                    appId      : '509855929050339', // App ID
                    status     : true, // check login status
                    cookie     : true, // enable cookies to allow the server to access the session
                    xfbml      : true  // parse XFBML
                });

                FB.Event.subscribe('auth.login', function () {
                    window.location = "http://webs.hogent.be/~096506gd/home.html";
                });
                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;
                        window.location = "http://webs.hogent.be/~096506gd/home.html";
                    } 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.
                    }
                });

            };

            // 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));
        </script>
        <div class="container">
            <img class="offset-by-three ten columns margin-top25" src="images/logo.png">
            <div class="offset-by-five">
                <a class="six columns full-width button margin-top175" href="home.html">
                    Aanmelden
                </a>
                <a class="six columns full-width button margin-top175" onclick="loginUser()">
                    Aanmelden met facebook
                </a>
            </div>
        </div>
    </body>
</html>

and this is the javascript file:

这是javascript文件:

function loginUser(){
    FB.login(function(response) {

        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            //console.log(response); // dump complete info
            access_token = response.authResponse.accessToken; //get access token
            user_id = response.authResponse.userID; //get FB UID

            FB.api('/me', function(response) {
                alert ("email: "+response.email); //get user email
                alert ("naam : "+respone.name);

          // you can store this data into your database             
            });

        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');

        }
    }, {
        scope: 'publish_stream,email'
    });
}

回答by Niraj Shah

In your code, you are including the Facebook JavaScript SDK three times! Remove the following lines: <script src="//connect.facebook.net/en_US/all.js"></script>and <script id="facebook-jssdk" async="" src="//connect.facebook.net/en_US/all.js"></script>

在您的代码中,您三次包含 Facebook JavaScript SDK!删除以下行: <script src="//connect.facebook.net/en_US/all.js"></script><script id="facebook-jssdk" async="" src="//connect.facebook.net/en_US/all.js"></script>

Also, you may need to wait a second or two before Facebook is initialised, which could be another reason why you were getting the error.

此外,您可能需要在 Facebook 初始化之前等待一两秒钟,这可能是您收到错误的另一个原因。

Give it another go and hopefully it will work now.

再试一次,希望它现在可以工作。

回答by Robot Boy

Just put the app_id next to the js src

只需将 app_id 放在 js src 旁边

<script src="//connect.facebook.net/en_US/all.js&appId=xxxxxxxxxxxxxxx" type="text/javascript"></script>

<script src="//connect.facebook.net/en_US/all.js&appId=xxxxxxxxxxxxxxx" type="text/javascript"></script>

That worked for me.

那对我有用。