【问题标题】:Location not populated on Spring Social FacebookSpring Social Facebook 上未填充位置
【发布时间】:2016-06-03 12:15:31
【问题描述】:

我正在尝试在 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……但不太确定。
  • 我添加了user_location 权限。你指的是哪个网址?

标签: java facebook-graph-api spring-social spring-social-facebook


【解决方案1】:

我终于明白了。首先它不适用于PageOperations,但它适用于GraphApi

代码是

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

诀窍是指定第三个参数,它表示字段(您可以将多个字段指定为字符串)。现在页面已填充,您可以获取国家/地区,例如pg.getLocation().getCountry()

【讨论】:

    【解决方案2】:

    正如你所说,以下命令

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

    为响应提供来自参考的名称。

    这有 2 个公共方法:getId()getName()

    getLocation() 这样的一些方法取决于权限。如果任何用户允许您查看他的位置,那么您可以访问此方法。

    From documentation:

    获取位置

    公共参考 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. Java Code Examples for org.springframework.social.facebook.api.Page


    更新1:

    我认为您需要通过 Graph API 获得用户权限。然后,您可以访问您向特定用户请求的位置或其他信息。 Checking Current PermissionsHandling Missing Permissionsthis 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();
    

    用户权限可以通过this tutorial

    用户位置可以通过this tutorial.

    获得相关权限后,还可以获取访问位置、经度、纬度等信息。

    【讨论】:

    • 我已经阅读了文档,但我的问题是它不起作用。首先,您的代码无法编译,因为fb.userOperations().getUserProfile().getLocation() 的类型为Reference,除了getId() 之外,它没有上述任何方法。如果你想要 Location 类型的东西,你应该按照我在我的问题中所说的去做,但是 getCountry() 返回 null,正如我在上面所说的那样。
    • 我还提到我正在使用 Spring Social 1.1.4,它实际上是最新的稳定版本。
    • @AdrianBer 抱歉造成误会。请查看更新。
    • 我再次阅读了文档。但是我的问题是Location中的所有字段都是空的,除了idname
    • @AdrianBer 我已经更新并给出了如何通过 api 获得许可的政策。然后您可以访问您的用户授予的信息。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多