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?
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()
.
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:
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.
链接地址: http://www.djcxy.com/p/34526.html