Java 春季社交Facebook:如何从帖子中获取大照片?
Post对象具有属性getPicture()。它包含指向非常小(130×130)图像的url 如何了解Facebook帖子的整体情况 示例url:Java 春季社交Facebook:如何从帖子中获取大照片?,java,spring-social-facebook,Java,Spring Social Facebook,Post对象具有属性getPicture()。它包含指向非常小(130×130)图像的url 如何了解Facebook帖子的整体情况 示例url: https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13173717_10209376327474891_7842199861010585961_n.jpg?oh=d244df2db666e1d3be73cb7b76060337&oe=57A64C44 替换url中的s130x130没有帮助,因为这在新的G
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.uncategorizedapiepeiception:(#100)尝试访问节点类型(照片)上不存在的字段(照片)
编辑:我发现了一些有用的东西:
byte[]photo=graphApi.mediaOperations().getAlbumImage(post.getObjectId(),ImageType.NORMAL)代码>
但是现在我得到一个字节[]而不是url,所以现在我必须将图像存储在某个地方:(使用ImageType.LARGE而不是ImageType.NORMAL)
它返回CustomMultipartFile我没有使用Spring Social framework获取Facebook帖子完整图片的直接方法。我使用Facebook的graph 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);
我们怎样才能获得facebook.feedOperations()。因为它只返回小图片。并且没有选项设置ImageType.Maven org.springframework.social spring social facebook 2.0.3.RELEASE@AwanishKumar你解决了这个问题吗?我现在面临着同样的问题。ImageType是spring social的facebook API中的枚举。更多参考信息:@kavishmital是的,但如果我获取facebook.feedOperations()如何获取正常大小的图像?@Deniel Henao:我使用自定义代码解决了它。我添加代码作为参考答案,您可以根据需要自定义。feedOperations()的问题您解决了吗?
@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";
}
}