Ivan
Ivan

Reputation: 31

Getting Facebook user from access token (PHP SDK 3.0.1)

I have an application that utilizes it's own login and facebook login combined. I use my own sessions and cookie to remember the user. I want to remember facebook user but without offline_access, so i figured to set my own cookie for facebook users and when they return just get new access_token from facebook because they have already authorized the app.

I managed to do all of that when the user logins for the first time but when they come back and I detect a cookie and I get a valid new access_token I can't put it inside facebook session. I use PHP SDK 3.0.1 and there is a function

$this->fb->getUserFromAccessToken($token)

inside base_facebook.php but it just throws me a server error when I use it. (fb is my class that extends Facebook class and $token is the valid new token)

When I use

$this->fb->getUser()

it just returns 0.

I know that I could get the information from

https://graph.facebook.com/me?access_token=$access_token

but I need to put it somehow inside facebook session

Also, there is a function inside base_facebook.php that works for me

$this->fb->setAccessToken($access_token);

So I can put the token inside the class and it works but when I call getUser() afterwards it still returns 0

Any help appreciated Thanks!

Upvotes: 2

Views: 17170

Answers (2)

Tebe Tensing
Tebe Tensing

Reputation: 1286

getUserFromAccessToken() function doesn't take any arguments. The function returns the user ID from already set access token. The function definition of getUserFromAccessToken() is shown below.

protected function getUserFromAccessToken() {
    try {
        $user_info = $this->api('/me');
        return $user_info['id'];
    } catch (FacebookApiException $e) {
        return 0;
    }
}

If you wish to find out the user ID from the access token you will need to set the access token first and then call the function getUser(). The code is shown below.

$facebook->setAccessToken($access_token);
$user_id = $facebook->getUser();

Upvotes: 8

Sascha Galley
Sascha Galley

Reputation: 16091

Well, I had a similar problem: How to properly handle session and access token with Facebook PHP SDK 3.0?

You can see there how I handle the session. Since the new PHP SDK you somehow can't directly edit the session of Facebook. If you also get the new access token via signed request or the JS SDK, you maybe can use a similar approach as I did.

My issue with lost information about the user seems to be solved with the IE iFrame problem, but I'm not quite sure if my approach will also work with fb->getUser().

Upvotes: 0

Related Questions