Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FB.logout() called without an access token

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:

FB.logout() called without an access token.

I'm using the Facebook JavaScript SDK, and the code I've got to logout looks like this:

$(document).ready($(function () {     $("#fblogout").click(facebooklogout); }));  function facebooklogout() {     FB.logout(function (response) {     } )}; 

This is the logout code specified at the Facebook Developers Documentation just with a button being assigned the method on document.ready

Before this code I have the FB.init() method, that all runs fine.

If anyone's got a solution as to why FB.logout doesn't have an access token, it'd be appreciated.

like image 738
martinthebeardy Avatar asked Dec 08 '11 11:12

martinthebeardy


People also ask

What is Facebook user token?

Facebook access token is an opaque string which is used to identify the user, application, or page and can be applied by the application to make graph API calls. Getting token for Facebook page is absolutely free.

Is Facebook using OAuth?

OAuth is also used when giving third-party apps access to accounts like your Twitter, Facebook, Google, or Microsoft accounts. It allows these third-party apps access to parts of your account.


2 Answers

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.

like image 191
Balaji Birajdar Avatar answered Sep 19 '22 04:09

Balaji Birajdar


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>');                     });                 });             }         });     } 
like image 20
Ozzy Avatar answered Sep 23 '22 04:09

Ozzy