我需要将所有消息发送给标识访问令牌的用户。这是为了获得其他用户在帖子中发表的所有评论,以及在他们的专辑中发表的评论。还有他们在对话中收到的消息。
我完成了如下操作:
myRESTapi
getAllCommentsFromConversationsAfterThan方法解释如下:
@Override
public List<CommentEntity> getCommentsLaterThan(Date startDate, String accessToken) {
List<CommentEntity> comments = new ArrayList<CommentEntity>();
try {
logger.debug("Call Facebook API for accessToken : " + accessToken + " on thread: " + Thread.currentThread().getName());
FacebookClient facebookClient = new DefaultFacebookClient(accessToken, Version.VERSION_2_8);
// Get Information about access token owner
User user = facebookClient.fetchObject("me", User.class);
StreamUtils.concat(
StreamUtils.concat(
getAllCommentsFromPostsAfterThan(facebookClient, startDate, user),
getAllCommentsFromAlbumsAfterThan(facebookClient, startDate, user)
)
.map(comment -> facebookCommentMapper.facebookCommentToCommentEntity(comment)),
getAllCommentsFromConversationsAfterThan(facebookClient, startDate, user)
)
.collect(Collectors.toList());
logger.debug("Total Facebook comments : " + comments.size());
} catch (FacebookOAuthException e) {
logger.error(e.getErrorMessage());
throw new InvalidAccessTokenException(SocialMediaTypeEnum.FACEBOOK, accessToken);
} catch (Exception e) {
throw new GetCommentsProcessException(e);
}
return comments;
}
我通过Graph API Explorer工具获得了访问令牌。授予所有权限。此错误消息会发生异常。
private Stream<CommentEntity> getAllCommentsFromConversationsAfterThan(FacebookClient facebookClient, Date startDate, User user) {
Connection<Conversation> conversations = facebookClient.fetchConnection("me/conversations", Conversation.class);
return StreamUtils.asStream(conversations.iterator())
.flatMap(List::stream)
.flatMap(conversation -> {
Connection<Message> messages = facebookClient.fetchConnection(
conversation.getId() + "/messages", Message.class, Parameter.with("fields", "message,created_time,from,id"));
return StreamUtils.asStream(messages.iterator());
})
.flatMap(List::stream)
.filter(message
-> !message.getFrom().getId().equals(user.getId())
&& (startDate != null ? message.getCreatedTime().after(startDate) : true))
.map(message -> facebookMessageMapper.facebookMessageToCommentEntity(message));
}
该文档指出read_mailbox权限已过时。
我正在使用2.8版。是否可以从对话中获取消息?
提前致谢
答案 0 :(得分:0)
首先,您必须获取具有read_page_mailboxes权限的用户访问令牌。然后,您可以生成页面访问令牌。