facebook4s icon indicating copy to clipboard operation
facebook4s copied to clipboard

How do i retrieve the user if all I have is the accessToken

Open Olabayo opened this issue 6 years ago • 3 comments

My use case is retrieve accessToken from mobile app, which is sent to the server and server confirms the token by querying facebook graph api with the accessToken. Your code assumes I already have the user id, on mobile app all I do is retrieve accessToken which is fine because I want the server to validate the token and continue from there.

Olabayo avatar Sep 29 '19 20:09 Olabayo

@Olabayo Do you mean your want to retrieve data of your current user ?

vooolll avatar Sep 30 '19 10:09 vooolll

@vooolll Yes without having a user id, by using the accessToken.

Pulled from the facebook php sdk

$fb = new Facebook\Facebook([ 'app_id' => '{app-id}', 'app_secret' => '{app-secret}', 'default_graph_version' => 'v4.0', ]);

// Notice I can retrieve a user without requiring a user id as long as I have his accessToken try { // Returns a Facebook\FacebookResponse object $response = $fb->get('/me?fields=id,name', '{access-token}'); } catch(Facebook\Exceptions\FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch(Facebook\Exceptions\FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; }

$user = $response->getGraphUser();

echo 'Name: ' . $user['name'];

Olabayo avatar Sep 30 '19 11:09 Olabayo

@Olabayo oh, it is easy to solve just pass FacebookUserId("me") instead of FacebookUserId("someid"), and you will get your user, https://github.com/vooolll/facebook4s/blob/master/docs/user.md

vooolll avatar Sep 30 '19 13:09 vooolll