Spring Social Facebook中位置未填写

7

我正在尝试在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,但不幸的是,并非所有地方都按照名称约定城市,国家,某些地方只包括城市名称,不包括国家。另外,如何获取地理坐标?


我猜这可能是Spring Social用于从用户获取权限的URL,但不太确定。 - No One
我添加了 user_location 权限。你指的是哪个URL? - Adrian Ber
2个回答

3

我终于搞定了。首先,它不能与 PageOperations 一起使用,但可以与 GraphApi 一起使用。

而且代码如下:

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

关键是要指定第三个参数,它代表字段(您可以将多个字段作为字符串指定)。现在页面已经填充,您可以像这样获取国家:pg.getLocation().getCountry()


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”权限下可用。

返回:用户位置的引用(如果可用)

代码示例:

  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);

资源链接:

  1. Location class
  2. org.springframework.social.facebook.api.Page的Java代码示例


更新1:

我认为您需要通过Graph API获取用户权限。然后,您可以访问特定用户请求的位置或其他信息。this link中提供了“检查当前权限”和“处理缺失权限”的方法。

关于用户权限:

/* 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();

关于用户权限,您可以参考这个教程

关于用户位置,您可以参考这个教程

在获取相关权限之后,您还可以获取位置、经度、纬度和其他信息。


我已经阅读了文档,但我的问题是它不起作用。首先,你的代码无法编译,因为fb.userOperations().getUserProfile().getLocation()Reference类型,除了getId()之外,没有上述任何方法。如果你想要Location类型的东西,你应该按照我在问题中所述的那样做,但是getCountry()返回null,正如我上面也说过的。 - Adrian Ber
我还提到了我正在使用Spring Social 1.1.4,这实际上是最新的稳定版本。 - Adrian Ber
@AdrianBer 抱歉误解了。请看更新后的内容。 - SkyWalker
我再次阅读了文档,但我的问题是Location中除了idname之外所有的字段都是空的。 - Adrian Ber
@AdrianBer 我已经更新并提供了如何通过API获取权限的策略。然后你就可以访问你被授予的用户信息。谢谢。 - SkyWalker
我有 user_location 权限,但它仍然不起作用。 - Adrian Ber

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接