首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring社交脸书|从feedOperations获取大照片

Spring社交脸书|从feedOperations获取大照片
EN

Stack Overflow用户
提问于 2017-04-07 19:16:37
回答 1查看 123关注 0票数 1

当我执行下面的代码时,我只得到了很小的图片。

代码语言:javascript
复制
PagingParameters recordCount = new PagingParameters(2000, null, null, null);
PagedList<Post> posts = facebook.feedOperations().getPosts(recordCount);

Maven版本:

代码语言:javascript
复制
<dependency
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId> <version>2.0.3.RELEASE</version> 
</dependency>

我们如何才能获得提要操作的全貌。对此有什么选择吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-04 21:48:02

我为这个问题分析了许多和发布解决方案,因为其他人可以从中获得帮助。它返回完整图片作为post额外数据的一部分,可以使用post.getExtraData().get("full_picture")获取这些数据。代码快照如下所示

代码语言:javascript
复制
public PagedList<Post> getPostByAccountId(final String accountId) {
    LOGGER.debug("facebook accountId: ", accountId);
    FacebookTemplate facebook = new FacebookTemplate("<access 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" };

    URIBuilder uriBuilder = URIBuilder.fromUri(facebook.getBaseGraphApiUrl() + "me/posts");
    uriBuilder = uriBuilder.queryParam("limit", String.valueOf(2000));
    uriBuilder.queryParam("fields", org.springframework.util.StringUtils.arrayToCommaDelimitedString(ALL_POST_FIELDS));
    URI uri = uriBuilder.build();

    JsonNode jsonNode = (JsonNode) facebook.getRestTemplate().getForObject(uri, JsonNode.class);
    PagedList<Post> posts = new DeserializingPosts().deserializeList(jsonNode, null, Post.class);
    return posts;
}

从post中获取full_picture。

代码语言:javascript
复制
String picture = post.getPicture(); 
picture = post.getExtraData().get("full_picture") != null ? post.getExtraData().get("full_picture").toString() : picture;

deserializingObject类详细信息:

代码语言:javascript
复制
public class DeserializingPosts extends AbstractOAuth2ApiBinding {

private ObjectMapper objectMapper = new ObjectMapper();

/**
 * 
 * @param jsonNode
 * @param postType
 * @param type
 * @return
 */
public <T> PagedList<T> deserializeList(JsonNode jsonNode, String postType, Class<T> type) {
    JsonNode dataNode = jsonNode.get("data");
    List posts = new ArrayList();
    for (Iterator iterator = dataNode.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);
}

/**
 * 
 * @param postType
 * @param type
 * @param node
 * @return
 */
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);
    }
}

/**
 * 
 * @param node
 * @return
 */
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) {
            return "post";
        }
    }
    return "post";
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43276785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档