2016-05-10 52 views
4

Post对象有一个属性getPicture()。这包含一个非常小的(130×130)图像的网址。春季社交Facebook:如何从邮政获得大照片?

如何获取Facebook邮件的全貌?

样品网址:

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

它无助于替换URL的s130x130,因为这将不是新的图形API的工作。

我试图使用graphApi.mediaOperations()但我没有看到接受postId的方法。有graphApi.mediaOperations().getPhotos(objectID)但这的objectID有ALBUMID或用户名根据文档,并且此方法抛出一个异常:

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

编辑:我发现一些作品:

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

但现在我得到一个字节[]而不是一个网址,所以现在我必须将图像存储在某处:(

+0

您是否解决了FeedOperations()的问题? –

回答

-1

使用ImageType.LARGE而不是ImageType.NORMAL 它返回CustomMultipartFile

+0

我们怎样才能得到facebook.feedOperations()。因为它只返回小图片。并且没有设置ImageType的选项。 Maven的 \t \t \t org.springframework.social \t \t \t 弹簧社会的Facebook \t \t \t 2.0.3.RELEASE \t \t

+0

@AwanishKumar你解决这个问题?我现在面对同样的事情。 –

+0

ImageType是Spring社交Facebook API中的枚举。 更多引用在这里:http://cleanbugs.com/item/412720/spring-social-how-get-big-photo-from-post-to-facebook.html –

0

我没有得到任何直接的方法来获取Facebook的full_picture使用Spring社会框架。我使用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); 

然后jsonNode将代码转换为您所需的格式。或者您也可以使用以下DeserializingPosts类将其投射到PagedList<Post>

@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"; 
    } 

} 
相关问题