How to retrieve user country using facebook Graph API?

前端 未结 4 1132
南旧
南旧 2020-12-30 03:13

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 coun

相关标签:
4条回答
  • 2020-12-30 03:27

    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"
    }
    
    0 讨论(0)
  • 2020-12-30 03:30

    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"
    }
    
    0 讨论(0)
  • 2020-12-30 03:36

    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}}"
    
    0 讨论(0)
  • 2020-12-30 03:44

    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.

    0 讨论(0)
提交回复
热议问题