7

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?

Community
  • 1
  • 1
Adrian Ber
  • 20,474
  • 12
  • 67
  • 117

2 Answers2

3

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().

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
0

As you stated that, the following command

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

gives response the name which is from Reference.

That's have 2 public methods: getId() and getName().

Some methods like getLocation() is depends on permission. If any user gives you permission to see his location, then you can access this method.

From documentation:

getLocation

public Reference getLocation()

The user's location. Available only with "user_location" or "friends_location" permission.

Returns: a Reference to the user's location, if available

Code Sample:

  Page profile=facebookTemplate.pageOperations().getPage(id);
  String name=profile.getName();
  String webside=profile.getWebsite();
  String logo="http://graph.facebook.com/" + id + "/picture";
  party=new FullPoliticalParty(name,color,logo,webside);
  String phone=profile.getPhone() == null ? "No disponible" : profile.getPhone();
  party.setPhone(phone);
  org.springframework.social.facebook.api.Location loc=profile.getLocation();
  if (loc != null) {
    location=new LocationMapa(loc.getCity(),loc.getCountry(),loc.getStreet(),loc.getZip());
  }
  party.setLocation(location);

Resource Link:

  1. Location class
  2. Java Code Examples for org.springframework.social.facebook.api.Page


UPDATE1:

I think you need user permissions through Graph API. Then you can access location or other info that you requested from specific user. Checking Current Permissions and Handling Missing Permissions is given in this link.

For USER permissions:

/* make the API call */
new GraphRequest(
    AccessToken.getCurrentAccessToken(),
    "/{user-id}/permissions",
    null,
    HttpMethod.GET,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
        }
    }
).executeAsync();

For user permissions, you can go through this tutorial

For user location, you can go through this tutorial.

After getting related permission, you can get access location, longitude, latitude and other info also.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • I already read the documentation, but my problem was that it doesn't work. First of all your code does not compile because `fb.userOperations().getUserProfile().getLocation()` is of type `Reference` which does have none of the methods above, except `getId()`. If you want something of `Location` type, you should do as I stated in my question, but then `getCountry()` returns null, as I also stated above. – Adrian Ber Jun 07 '16 at 16:12
  • And I also mentioned that I'm using Spring Social 1.1.4, which is in fact the latest stable release. – Adrian Ber Jun 07 '16 at 16:16
  • @AdrianBer Sorry for misunderstanding. Please have a look updated. – SkyWalker Jun 07 '16 at 17:12
  • Again, I read the documentation. But my problem is that all the fields in `Location` are null, except `id` and `name`. – Adrian Ber Jun 07 '16 at 18:08
  • @AdrianBer I have updated and given the policy how to get permission through api. Then you can access your user granted info. Thanks – SkyWalker Jun 08 '16 at 02:40
  • I have `user_location` permission, but it still doesn't work. – Adrian Ber Jun 08 '16 at 07:20