17

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!

1
  • see link
    – Esty
    Aug 17, 2015 at 9:16

4 Answers 4

53

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.

5
  • location{location} is new syntax for me - I obviously haven't been around that much to observe these changes - could you please add a little more explanations about it's usage?
    – Lix
    Aug 20, 2015 at 14:52
  • I'm sorry but I don't understand how I can add more explanations about its usage... Is there something you did not understand?
    – vchabot
    Aug 21, 2015 at 15:48
  • So I understand here we tried it for 'me' but can I give a user's ID there and get the same? Feb 7, 2017 at 10:37
  • 1
    I think this location{location} is old syntax. If someone is confused by it on 2017, what worked for me was: location.fields(location). See this topic for more details: stackoverflow.com/a/29274998/2561091 Aug 23, 2017 at 12:44
  • where is state? Nov 1, 2017 at 11:33
29

I believe the parameter you are looking for is location instead of country.

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

In order to query this data, you'll need your users to grant you the user_location permission.

This will give you value of the user submitted field - take note that this parameter might not always be populated since it depends on the user actually submitting this information - if they have not provided it - you will not be able to retrieve it.

The object will look something like this:

  "location": {
    "id": "112604772085346",
    "name": "Ramat Gan"
  },

Once you have the location object (which will most likely be a page), you can query that object to retrieve the country:

/112604772085346?fields=location

This will give you more information including the country.

{
  "location": {
    "city": "Ramat Gan",
    "country": "Israel",
    "latitude": 32.0833,
    "longitude": 34.8167,
    "zip": "<<not-applicable>>"
  },
  "id": "112604772085346"
}
4
  • Take a look at vchatbot's answer, @Idan - looks like there is a way to do this with only one request to the API - much better :P
    – Lix
    Aug 20, 2015 at 14:54
  • It is posible to get the ISO code for the country in facebook? en.wikipedia.org/wiki/ISO_3166-1 Mar 2, 2017 at 8:38
  • @user1253414 - I don't think that Facebook provides countries by ISO codes. You might need to use an additional step to convert these - perhaps something like this will help - github.com/lukes/ISO-3166-Countries-with-Regional-Codes
    – Lix
    Mar 2, 2017 at 8:44
  • @Lix Re your comment, as great as that source is, I've found 40 or so don't provide the country names. So It's bit of a tricky job matching up by name alone. Mar 9, 2019 at 6:26
2

Fields in Fb API are now concatenable, so, for a detailed info of User's location you need:

scope="public_profile,user_location"

fields="name,location{location{country, country_code, city, city_id, latitude, longitude, region, region_id, state, street, name}}"
1
  • Error: Tried accessing nonexisting field (user_location) Aug 15, 2019 at 6:22
0

v2.11 query:

/me?fields=hometown,location

permission:

user_hometown
user_location

result:

{
  "hometown": {
    "id": "XXXXREDACTED",
    "name": "Manila, Philippines"
  },
  "location": {
    "id": "XXXXREDACTED",
    "name": "Manila, Philippines"
  },
  "id": "XXXXREDACTED"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.