5

The Post object has a property getPicture(). This contains an url to a very small (130 × 130) image.

How to get the big picture of a Facebook post?

Sample url:

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

It does not help to replace the s130x130 in the url because that won't work in the new Graph API.

I tried to use graphApi.mediaOperations() but I don't see a method that accepts a postId. There is graphApi.mediaOperations().getPhotos(objectID) but this objectID has to be an AlbumID or UserID according to the documentation and this method throws an exception:

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

Edit: I found something that works:

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

But now I get a byte[] instead of an url so now I have to store the image somewhere :(

Roel
  • 3,089
  • 2
  • 30
  • 34

2 Answers2

0

i didn't get any direct method to fetch full_picture of Facebook post using Spring Social framework. I used Facebook's graph API to get full picture. I am adding code for references only. you need to be customize as per you need.

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 code be cast into your required format. or you can also cast it to PagedList<Post> using below 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";
    }

}
Awanish Kumar
  • 640
  • 8
  • 15
-1

use ImageType.LARGE instead of ImageType.NORMAL it returns CustomMultipartFile

jarvo69
  • 7,908
  • 2
  • 18
  • 28
  • How could we get for facebook.feedOperations(). since it returning only small picture. And there is no option to set ImageType. Maven org.springframework.social spring-social-facebook 2.0.3.RELEASE – Awanish Kumar Apr 07 '17 at 10:53
  • @AwanishKumar did you solve the issue? i'm facing the same thing right now. – Daniel Henao Jul 29 '17 at 15:11
  • ImageType is an enum in Spring social's Facebook API. More Reference here: http://cleanbugs.com/item/412720/spring-social-how-get-big-photo-from-post-to-facebook.html – jarvo69 Jul 29 '17 at 18:03
  • @KavishMittal yes, it is, but if I get the facebook.feedOperations() how can i get a normal size image? – Daniel Henao Aug 01 '17 at 23:40
  • @Deniel Henao: i solved it using custom code. I am adding code as an answer for references, you can customize according to your need. – Awanish Kumar Aug 03 '17 at 11:58
  • String [] fields = { "id", "email", "first_name", "last_name", "picture.width(500).height(500)" }; return facebook.fetchObject("me", User.class, fields); – Ashish Awasthi Jul 15 '18 at 13:22