FB.logout() called without an access token

前端 未结 6 1805
谎友^
谎友^ 2020-11-29 03:10

I\'m trying to log out of a website i\'ve created with Facebook integrated. Logging in works fine, but when I want to log out Firebug consistently gives me this error:

相关标签:
6条回答
  • 2020-11-29 03:50

    I went for the less trivial solution:

        function facebookLogout(){
            FB.getLoginStatus(function(response) {
                if (response.status === 'connected') {
                    FB.logout(function(response) {
                        // this part just clears the $_SESSION var
                        // replace with your own code
                        $.post("/logout").done(function() {
                            $('#status').html('<p>Logged out.</p>');
                        });
                    });
                }
            });
        }
    
    0 讨论(0)
  • 2020-11-29 03:51

    I've tried something like this:

    function fbLogout(){
        if(typeof FB.logout == 'function'){
            if (FB.getAuthResponse()) {
             FB.logout(function(response) { window.location.href = PROJECT_PATH + '/index/logout'; }); 
             return;
            }  
        };
    
        window.location.href = PROJECT_PATH + '/index/logout'; 
        return;  
    }
    
    0 讨论(0)
  • 2020-11-29 03:57

    Figured it out after so many tries. Actually, we are passing response to FB.logout.

    Generally response.authResponse.accessToken contains token. So, its error about the accessToken not being there.

    Think logically, where does that response come from in your code? Out of nowhere.

    So, we need to get that response object from a function and pass that to logout function. I don't know how it worked for others, but this worked for me.

    Just replace the code with this

    function logout(){
      FB.getLoginStatus(function(response) {
        FB.logout(function(response){
          console.log("Logged Out!");
          window.location = "/";
        });
      });
    }
    

    What we do here is, get the login status if the user is logged in and get the corresponding response in return, which contains all the necessary tokens and data. And, then we pass this response to logout function.

    0 讨论(0)
  • 2020-11-29 04:01

    To logout from the application which uses facebook graph API, use this JavaScript on the logout page just after the <form> tag:

    window.onload=function()
    {
        // initialize the library with your Facebook API key
        FB.init({ apiKey: 'b65c1efa72f570xxxxxxxxxxxxxxxxx' });
    
        //Fetch the status so that we can log out.
        //You must have the login status before you can logout,
        //and if you authenticated via oAuth (server side), this is necessary.
        //If you logged in via the JavaScript SDK, you can simply call FB.logout()
        //once the login status is fetched, call handleSessionResponse
        FB.getLoginStatus(handleSessionResponse);
    }
    
    //handle a session response from any of the auth related calls
    function handleSessionResponse(response) {
        //if we dont have a session (which means the user has been logged out, redirect the user)
        if (!response.session) {
            window.location = "/mysite/Login.aspx";
            return;
        }
    
        //if we do have a non-null response.session, call FB.logout(),
        //the JS method will log the user out of Facebook and remove any authorization cookies
        FB.logout(handleSessionResponse);
    }
    

    The code works and is live on my site.

    0 讨论(0)
  • 2020-11-29 04:06

    Should be something more like this. There was a change to the JS API where you have to use authResponse instead of just session.

    //handle a session response from any of the auth related calls
    function handleSessionResponse(response) {
    
        //if we dont have a session (which means the user has been logged out, redirect the user)
        if (!response.authResponse) {
            return;
        }
    
        //if we do have a non-null response.session, call FB.logout(),
        //the JS method will log the user out of Facebook and remove any authorization cookies
        FB.logout(response.authResponse);
    }
    
    0 讨论(0)
  • 2020-11-29 04:10

    The error says that you don't have an access token, you have to check for one using the FB.getAccessToken() function.

    If there is no access token the function returns null. See example below:

       function facebooklogout() {
        try {
            if (FB.getAccessToken() != null) {
                FB.logout(function(response) {
                    // user is now logged out from facebook do your post request or just redirect
                    window.location.replace(href);
                });
            } else {
                // user is not logged in with facebook, maybe with something else
                window.location.replace(href);
            }
        } catch (err) {
            // any errors just logout
            window.location.replace(href);
        }
       }
    
    0 讨论(0)
提交回复
热议问题