javascript - facebook login error FB.login() called before FB.init() - Stack Overflow

i have a problem with loggin in into facebook i keep getting the error code: FB.login() called before F

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:

 <!-- Javascript
        =================================================== -->
        <script src="//connect.facebook/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 = ".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 = ".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/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:

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

        if (response.authResponse) {
            console.log('Wele!  Fetching your information.... ');
            //console.log(response); // dump plete 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'
    });
}

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:

 <!-- Javascript
        =================================================== -->
        <script src="//connect.facebook/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/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:

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

        if (response.authResponse) {
            console.log('Wele!  Fetching your information.... ');
            //console.log(response); // dump plete 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'
    });
}
Share Improve this question asked May 11, 2013 at 15:20 Glenn DeryckeGlenn Derycke 1292 gold badges4 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

Give it another go and hopefully it will work now.

Just put the app_id next to the js src

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

That worked for me.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744405329a4572600.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信