Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook getLogoutUrl returns a URL with access_token=0, when clicked redirects to Facebook user's home page

I'm integrating Facebook with my site and I've added a Logout button whose URL is taken from:

$facebook->getLogoutUrl(array('next' => 'http://mydomain.com/logout.php'));

The problem is that logout.php is never called. Instead, on click of the Logout button, it redirects to the logged-in user's Facebook home page. It doesn't log the user out of Facebook, and it doesn't call my next URL.

I noticed that the URL generated by getLogoutURL() looks like:

https://www.facebook.com/logout.php?next=http://mydomain.com/logout.php&access_token=0

Notice there is an access_token=0. Should that value not be zero? That's the only thing I can think of that might be causing the problem.

I've already set my FB app's Site URL to http://mydomain.com. While testing locally, I've also edited my hosts file. I've also googled a lot and I haven't found a solution. The only one that worked was adding an onclick to my logout button using FB.logout(). But I would need to use PHP.

Any ideas as to why the logout URL is not working?

like image 763
Obay Avatar asked Mar 30 '13 19:03

Obay


People also ask

How long do Facebook access tokens last?

When your app uses Facebook Login to authenticate someone, it receives a User access token. If your app uses one of the Facebook SDKs, this token lasts for about 60 days. However, the SDKs automatically refresh the token whenever the person uses your app, so the tokens expire 60 days after last use.

What is an access token on Facebook?

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.

How can I get Facebook permanent access token?

To get this token, go to the Graph API Explorer of Facebook. Then, in the User or Page menu, select “User Token” (2). Finally click on “Get Access Token”, you will get a User Access Token.


4 Answers

Okay, I've solved this by creating my own logout URL and adding an access token

$logoutUrl = 'https://www.facebook.com/logout.php?next=http://mydomain.com/logout.php&access_token=' . $facebook->getAccessToken();
like image 167
Obay Avatar answered Oct 10 '22 23:10

Obay


I hope you've found a solution! But if not, try with the following code. It worked for me!

$facebook->getLogoutUrl(array('next' =>'http://example.com/logout.php', 'access_token'=>$facebook->getAccessToken()));`

If you check out the official documentation you'll see it says nothing about setting the access token option in the parameter array but it really works! Good Luck!!

like image 21
Fernando Prieto Avatar answered Oct 10 '22 22:10

Fernando Prieto


The problem is that you need to call getAccessToken BEFORE ask for the logout url.

$facebook->getAccessToken();

Don't need to call it inside as a getLogoutURL parameter. Just call it one line before:

$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$logoutUrl = $facebook->getLogoutUrl();
like image 36
Arvy Avatar answered Oct 10 '22 21:10

Arvy


I had the same problem too (access_token=0), but then I realized I was clearing Facebook cookies before calling getLogoutURL(). If you get the getLogoutURL() result first, access_token should not be zero.

like image 20
ScottyB Avatar answered Oct 10 '22 21:10

ScottyB