Spring Social Facebook:如何从 Post 获取大照片?

2024-01-03

Post 对象有一个属性 getPicture()。其中包含一个非常小的 (130 × 130) 图像的 URL。

如何获取 Facebook 帖子的整体情况?

示例网址:

https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13173717_10209376327474891_7842199861010585961_n.jpg?oh=d244df2db666e1d3be73cb7b76060337&oe=57A64C44

替换 url 中的 s130x130 没有帮助,因为这在新的 Graph API 中不起作用。

我尝试使用graphApi.mediaOperations()但我没有看到接受 postId 的方法。有graphApi.mediaOperations().getPhotos(objectID)但根据文档,此 objectID 必须是 AlbumID 或 UserID,并且此方法会引发异常:

org.springframework.social.UncategorizedApiException: (#100) Tried accessing nonexisting field (photos) on node type (Photo)

Edit:我发现了一些有用的东西:

byte[] photo = graphApi.mediaOperations().getAlbumImage(post.getObjectId(), ImageType.NORMAL);

但现在我得到一个 byte[] 而不是 url,所以现在我必须将图像存储在某个地方:(


我没有得到任何直接的方法来使用 Spring Social 框架获取 Facebook 帖子的 full_picture。我使用 Facebook 的图形 API 来了解全貌。我添加的代码仅供参考。您需要根据您的需要进行定制。

FacebookTemplate facebook = new FacebookTemplate("<fb token>");

String[] ALL_POST_FIELDS = { "id", "actions", "admin_creator", "application", "caption", "created_time", "description", "from", "icon",
        "is_hidden", "is_published", "link", "message", "message_tags", "name", "object_id", "picture", "full_picture", "place", "privacy",
        "properties", "source", "status_type", "story", "to", "type", "updated_time", "with_tags", "shares", "likes.limit(1).summary(true)" };

URIBuilder uriBuilder = URIBuilder.fromUri(facebook.getBaseGraphApiUrl() + request.getAccountId() + "/posts");
uriBuilder = uriBuilder.queryParam("limit", String.valueOf(request.getRecordCount()));
uriBuilder.queryParam("fields", org.springframework.util.StringUtils.arrayToCommaDelimitedString(ALL_POST_FIELDS));
URI uri = uriBuilder.build();
LOGGER.info("facebook URL :{} ", uri);
JsonNode jsonNode = (JsonNode) facebook.getRestTemplate().getForObject(uri, JsonNode.class);
LOGGER.debug("facebook URL :{}, response: {} ", uri, jsonNode);
// you can cast jsonnode as required into your format or below line can be used to cast into PagedList<Post> format
PagedList<Post> posts = new DeserializingPosts().deserializeList(jsonNode, null, Post.class, true);

Then jsonNode代码被转换成您需要的格式。或者你也可以将其投射到PagedList<Post>使用下面DeserializingPosts class.

@Component
public class DeserializingPosts extends AbstractOAuth2ApiBinding {

    private ObjectMapper objectMapper = new ObjectMapper();

    private static final Logger LOGGER = Logger.getLogger(DeserializingPosts.class);

    public <T> PagedList<T> deserializeList(JsonNode jsonNode, String postType, Class<T> type, boolean accountFlag) {
        JsonNode dataNode = jsonNode.get("data");
        return deserializeList(dataNode, postType, type);
    }


    public <T> PagedList<T> deserializeList(JsonNode jsonNode, String postType, Class<T> type) {
        List posts = new ArrayList();
        for (Iterator iterator = jsonNode.iterator(); iterator.hasNext();) {
            posts.add(deserializePost(postType, type, (ObjectNode) iterator.next()));
        }
        if (jsonNode.has("paging")) {
            JsonNode pagingNode = jsonNode.get("paging");
            PagingParameters previousPage = PagedListUtils.getPagedListParameters(pagingNode, "previous");
            PagingParameters nextPage = PagedListUtils.getPagedListParameters(pagingNode, "next");
            return new PagedList(posts, previousPage, nextPage);
        }

        return new PagedList(posts, null, null);
    }


    public <T> T deserializePost(String postType, Class<T> type, ObjectNode node) {
        try {
            if (postType == null) {
                postType = determinePostType(node);
            }

            node.put("postType", postType);
            node.put("type", postType);
            MappingJackson2HttpMessageConverter converter = super.getJsonMessageConverter();
            this.objectMapper = new ObjectMapper();
            this.objectMapper.registerModule(new FacebookModule());
            converter.setObjectMapper(this.objectMapper);
            return this.objectMapper.reader(type).readValue(node.toString());
        } catch (IOException shouldntHappen) {
            throw new UncategorizedApiException("facebook", "Error deserializing " + postType + " post" + shouldntHappen.getMessage(),
                    shouldntHappen);
        }
    }

    private String determinePostType(ObjectNode node) {
        if (node.has("type")) {
            try {
                String type = node.get("type").textValue();
                Post.PostType.valueOf(type.toUpperCase());
                return type;
            } catch (IllegalArgumentException e) {
                LOGGER.error("Error occured while determining post type: " + e.getMessage(), e);
                return "post";
            }
        }
        return "post";
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Social Facebook:如何从 Post 获取大照片? 的相关文章

随机推荐

  • PIWIK GeoIP (PECL) 给出未知位置

    我已经在我的系统上成功安装了 piwik 但 geoIP 定位不起作用 我已遵循 piwik 的指南FAQ http piwik org faq how to faq 164 通过 PECL 扩展配置 Geo IP 地理定位 但总是显示 根
  • 一个变量的多个比较运算符?

    我需要对一个变量进行多次检查 我在这里看到了一个 等于 的例子 w3学校 http www w3schools com php php operators asp 但它们是两个不同的变量 现在我有 if color blue do some
  • strcmp() 的不明确行为

    请注意 我已经检查了与该标题相关的问题 但从我的角度来看 它们与该问题无关 最初我认为program1和program2会给我相同的结果 Program 1 char a abcd char b efgh printf d strcmp a
  • 从后面的代码调用jquery

    您好 我有一个 jquery 函数 单击按钮时执行该函数 我还需要根据项目是否附加注释从后面的代码执行此函数 这是jquery Comments Slide commentsnr live click function up to pare
  • 有效地在列表中查找唯一的向量元素

    我有一个数值向量列表 我需要创建一个仅包含每个向量的一个副本的列表 没有相同函数的列表方法 因此我编写了一个函数来应用来检查每个向量 F1 lt function x to remove lt c for i in 1 length x f
  • 句柄到窗口句柄

    我尝试过使用 获取桌面枚举的所有进程 ID 方法 但这不起作用 有没有办法将句柄转换为窗口句柄 或者 有没有办法获取进程 ID 并找出该进程生成的所有子窗口 我不想使用FindWindow由于多个流程问题 你可以打电话枚举Windows h
  • Grep 匹配之前和之后的字符?

    使用这个 grep A1 B1 test pattern file 将在文件中匹配的模式之前和之后生成一行 有没有办法不显示行而是显示指定数量的字符 我的文件中的行非常大 因此我对打印整行不感兴趣 而只是观察上下文中的匹配 关于如何执行此操
  • 如何获取运行 Node.js 的服务器名称?

    我想添加一行 该行将指向不同的数据库 具体取决于我运行 node js 的服务器 localhost 与 test com 等 如何获取node js中的服务器名称 即服务器端相当于location host的是什么 var os requ
  • 为什么Thread.sleep(0)可以阻止rocketmq中的gc?

    最近我阅读了RocketMQ的源代码 但我无法理解这段代码 为什么这段代码可以阻止gc呢 https github com apache rocketmq blob master store src main java org apache
  • 是什么导致 .Attach() 在 EF4 中运行缓慢?

    我们的代码中有一个通用的更新方法 它执行以下操作 foreach var entity in entityList Context GetIDbSet
  • 未应用 Azure AD 可配置令牌生命周期

    我想更改 Azure AD 中的默认令牌生命周期如图所示 https learn microsoft com en us azure active directory active directory configurable token
  • 谷歌电子表格脚本,为一个单元格提供多个超链接选择

    我有一个谷歌电子表格 在某些单元格中 它有多个名称 字符串 我想将它们与各个超链接关联起来 例如 如果我有一个像 Charles Darwin 这样的单元格 我可以很容易地通过执行以下操作来创建该名称的超链接 Hyperlink VLOOK
  • 宏扩展和字符串化:如何使用另一个宏将宏名称(而不是其值)字符串化?

    不感兴趣 define ACD 5 5 5 30 define DEFAULT NETWORK TOKEN KEY CLASS ACD define DEFAULT NETWORK TOKEN KEY DEFAULT NETWORK TOK
  • 使用外部数据使用 pyqtgraph 进行绘图

    我正在尝试实时绘制不同传感器的数据 因此我决定使用 PyQt 中的 PyQtGraph 绘制数据 以便使其能够处理来自不同来源的多个传感器的数据 在互联网上搜索示例 我找到了一个并尝试对其进行修改 因为 QtGui QApplication
  • R:具有 0 和 NA 的矩阵的倍数,从而产生不同的结果

    我想将 df 与矩阵相乘 df 具有 0 值 矩阵具有 NA 值 这一点很重要 通过将 0 在 df 中 与一个值 在 mat 中 相乘 我得到 0 将一个值 在 df 中 与 NA 在 mat 中 相乘 我得到 NA 这对我来说是正确的并
  • 直接访问 CGRect 值与在 Swift 中标准化它们 - Objective-C 规则仍然有效吗?

    这个问题的灵感来自于安德鲁 卡特的评论 https stackoverflow com questions 34981995 whats the difference between using cgsizemake and cgsize
  • 从 .vdproj 构建没有 Devenv 的合并模块

    我阅读了一些有关通过命令行构建合并模块的 Stackoverflow 问题 但当有人建议使用 devenv 进行编译或使用 Dark 从现有 msi 文件创建 wix 文件时 所有这些问题都被接受了 考虑以下因素 我必须使用的构建服务器上未
  • 将新的 Microsoft Edge 添加到 Web 浏览器控件?

    带有 Microsoft Edge 的新 Windows 10 已经到来 如何将其添加到我的网络浏览器控件中 我需要它是因为实际的 Web 浏览器控件不允许 JavaScript 和 CSS3 我曾经还使用 WebKit 和 Awesomi
  • Visual Studio 发布 Web 对话需要过多时间才能加载

    每当我启动 Visual Studio 2015 发布 Web 对话 或 Visual Studio 2013 两者都有相同的问题 时specific项目 打开它需要大约 20 30 秒 同样 当我在发布配置文件之间切换时 需要相同的时间当
  • Spring Social Facebook:如何从 Post 获取大照片?

    Post 对象有一个属性 getPicture 其中包含一个非常小的 130 130 图像的 URL 如何获取 Facebook 帖子的整体情况 示例网址 https scontent xx fbcdn net v t1 0 0 s130x