Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve user country using facebook Graph API?

I want to retrieve the logged in user country, and insert it into a div. I succeed making it with the user Name, Gender and Age range, but somewhy I can't retrieve the country. Here is my code:

  function testAPI() {
console.log('Welcome!  Fetching your information.... ');
FB.api('/me?fields=name,email,gender,age_range,picture,country', function (response) {
  document.getElementById('status').innerHTML = response.name + ",";
  document.getElementById('status1').innerHTML = response.gender + ",";
  document.getElementById('status2').innerHTML = (response.age_range.min) + "-" + (response.age_range.min + 1) + " years old";
  document.getElementById('status3').innerHTML = response.country;
}

Thanks!

like image 923
Idan Avatar asked Aug 17 '15 09:08

Idan


1 Answers

To complete what Lix said:

You can do it with only one call to Graph API, by calling

/me?fields=name,email,gender,age_range,picture,location{location}

Indeed, the location object under the user object is a Page object, which has a Location object.

So, this will give you something like this:

"location": {
  "location": {
    "city": "Ramat Gan",
    "country": "Israel",
    "latitude": 32.0833,
    "longitude": 34.8167,
    "zip": "<<not-applicable>>"
  }
}

This avoids you to make a double call to Graph API.

like image 128
vchabot Avatar answered Oct 13 '22 00:10

vchabot