Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location not populated on Spring Social Facebook

I'm trying to get user location in Spring Social Facebook:

Facebook fb = ((Facebook) connection.getApi());
Page pg = fb.pageOperations().getPage(fb.userOperations().getUserProfile().getLocation().getId());

The problem is that pg.getLocation() returns null.

I also tried with

fb.fetchObject(fb.userOperations().getUserProfile().getLocation().getId(), org.springframework.social.facebook.api.Location.class)

but it does populate only the name, not country or other fields.

Trying on Graph API Explorer /v2.6/112286918797929?fields=id,name,location returns as expected:

{
  "id": "112286918797929",
  "name": "Sacele",
  "location": {
    "city": "Sacele",
    "country": "Romania",
    "latitude": 45.6167,
    "longitude": 25.6833
  }
}

Is this an issue with Spring Social Facebook?

I'm using Spring Social 1.1.4 and Spring Social Facebook 2.0.3.

I also tried Spring Social for Facebook - get user location, but unfortunately not all the places follow the name convention City, Country and some of them include in the name only the city, not the country too. Plus how can you get the geo-coordinates?

like image 783
Adrian Ber Avatar asked Jun 03 '16 12:06

Adrian Ber


1 Answers

I finally got it. First of all it doesn't work with PageOperations, but it works with GraphApi.

And the code is

UserProfile userProfile = fb.userOperations().getUserProfile();
Page pg = fb.fetchObject(userProfile.getLocation().getId(), Page.class, "location");

The trick is specifying the third parameter, which represents the fields (you can specify multiple fields as strings). Now the page is populated and you can get the country like pg.getLocation().getCountry().

like image 85
Adrian Ber Avatar answered Sep 18 '22 01:09

Adrian Ber