简体   繁体   中英

Error in the response from facebook graph API

My code till now is this:

<script>
    window.fbAsyncInit = function(){
        FB.init({ appId:'my app id', status:true,  cookie:true, xfbml:true});

        FB.getLoginStatus(function(response){
            if (response.status != "unknown") // makes sure the user is already logged in.
            {
                FB.api('/me', function(response) {
                console.log(response);
                });
            }
        });
    };
    // 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>

The error that I'm getting in the console is "An active access token must be used to query information about the current user." But I have already made sure that the user is logged in as seen from the code, then where am I making a mistake?

Update: From the answers and the comments, it seems like the problem is that my website is not authorized although the user is logged in. I just want to display the user's real name on the website with a welcome message. Can I use facebook graph API for this? I do not want to go into authorization step since that would seem unnecessary from the user's point of view.

I'm new to programming, javascript and facebook graph api, so please forgive any mistakes I might have made.

Where did you make sure the user is logged in? You did not authorize the user anywhere, you only checked if he is logged in. For example:

FB.login(function(response) {
    if (response.authResponse) {
        //user just authorized your app

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

You may want to use getLoginStatus correctly too:

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        //user is authorized

    } else {
        //user is not authorized

    }
});

Source: http://www.devils-heaven.com/facebook-javascript-sdk-login/

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