40

I've developed a WordPress plugin for social login using Facebook. I'm using the Facebook graph API /me to retrieve the user details. My problem is that for some websites, when installed a Facebook login plugin, I'm only getting user ID and name.

Array
(
    [name] => John doe
    [id] => 398463877009801
)

but same code is working well for some websites as well.

Array
(
    [id] => 398463877009801
    [email] => something@gmail.com
    [first_name] => John
    [gender] => male
    [last_name] => Doe
    [link] => https://www.facebook.com/app_scoped_user_id/398463877009801/
    [locale] => en_US
    [name] => John Doe
    [timezone] => 5.45
    [updated_time] => 2015-05-03T11:24:16+0000
    [verified] => 1
)

What might be the possibilities of the errors for the site that is getting only name and id?

Maxime
  • 8,645
  • 5
  • 50
  • 53
Jay Maharjan
  • 566
  • 1
  • 4
  • 14

1 Answers1

76

As CBroe already pointed out in the above comment, the Facebook API - newer than version 2.4 - changed the response and the way the requests are being sent.

You have to specify each field you want to be returned from the Graph API within your request.

For example, if you want the fields email and name returned, you must add them inside the request like this:

/me?fields=email,name
Răzvan
  • 981
  • 9
  • 20
  • 1
    Hi Razvan Thank you so much for your reply. But I tried already placing the fields values but still i'm not getting what i want. I'm always receving id and name if I place the field attributes or not placing any field attributes. It's so strange behaviour to me because same code is working for most of the sites. Thanks – Jay Maharjan Jul 27 '15 at 04:19
  • Are you using the official php facebook sdk? Can we see some code? Did you ask for the right permissions by specifying the scope? – Răzvan Jul 27 '15 at 04:42
  • I don't know how to send the link of the files here. I'll try to narrate my code [code] $facebook = new Facebook($config); $user = $facebook->getUser(); if ($user){ try {// Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } print_r($user_profile ); [/code] – Jay Maharjan Jul 27 '15 at 04:57
  • First of all upgrade your SDK to this version [NEW FACEBOOK SDK](https://github.com/facebook/facebook-php-sdk-v4), as it seems to me that you are running an outdated version and follow this self-explanatory [EXAMPLE](https://github.com/facebook/facebook-php-sdk-v4/blob/5.0-dev/docs/example_facebook_login.fbmd). Note the $permissions array. – Răzvan Jul 27 '15 at 05:14
  • I really can't make the new facebook sdk work for me. Can you please show me some examples how to implement these example codes. – Jay Maharjan Jul 28 '15 at 05:59
  • Finally I was able to fix the issue after updating the facebook php sdk. – Jay Maharjan Aug 10 '15 at 11:23
  • 1
    Thank You!!!! buddy, I fixed this issue at my end, I used 3rd party plugin to manage the Facebook API. below is my call 'https://graph.facebook.com/v2.3/me?fields=id,name,first_name,last_name,email,gender,link,locale,timezone,updated_time,verified' – Raj Sep 07 '15 at 06:45