简体   繁体   中英

FB.Login response.session not working?

I'm having a problem with the following code. According to the JS, if the response.session is valid, it should alert("User is logged in!"); . The problem is, I'm pretty sure the session is valid, but it alerts "User is not logged in!". I know the session is valid, as I have logged in before, and allowed the app publish_stream when the window appeared. I have also tried removing the app from my FB, but when I allow it again, it still alerts "User is not logged in!". What is wrong with my code?

<html>
    <head>
        <title>My Facebook Login Page</title>
        <script src="//connect.facebook.net/en_US/all.js"></script>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                    appId      : '436956019655560',
                    status     : true, 
                    cookie     : true,
                    xfbml      : true,
                    oauth      : true,
                });
            };

            function fbLogin() {
                FB.login(function(response) {
                    if (response.session) {
                        console.log("User is connected to the application.");
                        var accessToken = response.session.access_token;
                        alert("User is logged in!");
                    } else {
                        alert("User is not logged in!");
                    }
                }, {scope:'publish_stream'});
            }
    </script>
    </head>
    <body>
        <div id="fb-root"></div>
        <INPUT TYPE="BUTTON" ONCLICK="fbLogin()" value="login">
    </body>
</html>

I'm pretty sure that response.session does not exist anymore. I believe you need to be looking for response.authResponse. Check out the docs on FB.login:

As of December 13th, 2011, the JavaScript SDK now only supports OAuth 2.0 for authentication. The ability to enable OAuth 2.0 in the JS SDK was first introduced in July. All apps were given until October 1, 2011 to test and migrate. With this change, please ensure that you replaced response.session with response.authResponse.

Try checking for response.authResponse instead. Also, try logging the response object to the console and see exactly what you are getting back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM