2016-05-10 54 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"; 
    } 

} 
相關問題