我正试图在Spring Social Facebook中获得用户位置:
Facebook fb = ((Facebook) connection.getApi());
Page pg = fb.pageOperations().getPage(fb.userOperations().getUserProfile().getLocation().getId());
问题是pg.getLocation()
会返回null
。
我也试过
fb.fetchObject(fb.userOperations().getUserProfile().getLocation().getId(), org.springframework.social.facebook.api.Location.class)
但它只填充名称,而不是国家或其他字段。
尝试使用Graph API Explorer /v2.6/112286918797929?fields=id,name,location按预期返回:
{
"id": "112286918797929",
"name": "Sacele",
"location": {
"city": "Sacele",
"country": "Romania",
"latitude": 45.6167,
"longitude": 25.6833
}
}
这是Spring Social Facebook的问题吗?
我正在使用Spring Social 1.1.4和Spring Social Facebook 2.0.3。
我也试过Spring Social for Facebook - get user location,但遗憾的是并非所有地方都遵循名称约定城市,国家/地区,其中一些地方仅包括城市名称,而不是国家名称。另外如何获得地理坐标?
答案 0 :(得分:3)
我终于明白了。首先,它不适用于PageOperations
,但它适用于GraphApi
。
代码是
UserProfile userProfile = fb.userOperations().getUserProfile();
Page pg = fb.fetchObject(userProfile.getLocation().getId(), Page.class, "location");
技巧是指定第三个参数,它表示字段(您可以将多个字段指定为字符串)。现在页面已填充,您可以获得pg.getLocation().getCountry()
。
答案 1 :(得分:0)
如你所述,以下命令
fb.fetchObject(fb.userOperations().getUserProfile().getLocation().getId(), org.springframework.social.facebook.api.Location.class)
给出响应名称,该名称来自参考。
这有两种公开方法:getId()
和getName()
。
getLocation()
等某些方法取决于权限。如果任何用户授予您查看其位置的权限,则可以访问此方法。
<强>的getLocation 强>
public reference getLocation()
用户的位置。仅适用于“user_location”或“friends_location”权限。
返回:a 参考用户的位置(如果有)
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);
我认为您需要通过Graph API获得用户权限。然后,您可以访问您从特定用户请求的位置或其他信息。 Checking Current Permissions
中提供了Handling Missing 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();
。
{{1}}
对于用户权限,您可以浏览this link
对于用户位置,您可以浏览this tutorial。
获得相关许可后,您还可以获取访问位置,经度,纬度和其他信息。