Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API - How to get Gender from Public Profile

I need to retrieve the gender from a Facebook user from his public profile via Facebook Graph API.

Unfortunately it is not always working. See these examples:

working: https://graph.facebook.com/1423993568

{
   "id": "1423993568",
   "gender": "male",
   ...
}

not working: https://graph.facebook.com/10152133878666403

{
   "error": {
      "message": "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
      "type": "GraphMethodException",
      "code": 100
   }
}

But when I open the user id directly via web, it works, and I can even see the gender. (Go to "About/Contact and Basic Info") I am logged in with my personal account and not a friend of this user! http://www.facebook.com/10152133878666403

What is the problem? Is it a problem with the request or a security feature from Facebook?

UPDATE:

I can do a workaround like this, but only if I am logged in with my browser:

Take the app_scoped_user_id and get the Facebook username from the redirect URL, when opening this request:

http://www.facebook.com/app_scoped_user_id/10152133878666403
  --> https://www.facebook.com/shiffoni
  --> https://graph.facebook.com/shiffoni

returns:

{
   "id": "669926402",
   "first_name": "Shiffoni",
   "gender": "female",
   "last_name": "Leonhardt",
   "locale": "de_DE",
   "name": "Shiffoni Leonhardt",
   "username": "shiffoni"
}

Any other ideas to get the gender, real userId, or nickname?

like image 582
electronix384128 Avatar asked Jul 24 '14 23:07

electronix384128


2 Answers

As already answered in the comments by a Facebook employee, it is simply not possible, because it is not supported to get the 'gender' or the real Facebook user id.

Possible workarounds:

  • use gender api (e.g. genderize.io) to retrieve gender based on first name.
  • scrape the gender information from the Facebook (need to be logged in via web) from the user about page.
like image 98
electronix384128 Avatar answered Sep 21 '22 13:09

electronix384128


Best Way to check it using facebook graph api under develoers.facebook.com => tools & support =>Graph API Explorer.

$fb = new Facebook(array(
 'appId'  => $app_id,
  'secret' => $app_secret,
  'cookie' => true
));

$fbuser=$fb->api('/me/friends?fields=id,name,gender');

it will give an array of your all friends with name & gender, you can loop through and put a condition inside the loop for separating the male & female friends.

Hope it helps

for more info read this article

Getting Facebook Friends with Gender

like image 42
Talib Hassan Avatar answered Sep 24 '22 13:09

Talib Hassan