Idan
Idan

Reputation: 223

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!

Upvotes: 17

Views: 21576

Answers (4)

Rodrigo Butta
Rodrigo Butta

Reputation: 409

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}}"

Upvotes: 2

xemasiv
xemasiv

Reputation: 87

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"
}

Upvotes: 0

vchabot
vchabot

Reputation: 581

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.

Upvotes: 54

Lix
Lix

Reputation: 47976

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"
}

Upvotes: 29

Related Questions