通过Spring Social发布带照片的推文

时间:2018-11-03 13:13:00

标签: spring twitter social

我正在尝试在Twitter上发布照片。

@Override
public void postOnTwitter(String uuid, String status, String key, String secret) throws AuthenticationException {

    Picture picture = pictureRepository.findByUuid(uuid)
            .orElseThrow(() -> new ResourceNotFoundException(getPictureNotFoundMessage(uuid)));

    TwitterTemplate twitter = new TwitterTemplate(TWITTER_API_KEY, TWITTER_API_KEY_SECRET, key, secret);

    PictureResource resource = new PictureResource();
    try {
        BufferedImage image = ImageIO.read(new File(picture.getCodedFilePath()));
        byte[] imageBytes = PictureUtils.bufferedImageToBytes(image, picture.getFormat());
        String base64 = Base64.getEncoder().encodeToString(imageBytes);
        resource.setData(base64);
        resource.setFileName(picture.getAuthor());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        TweetData tweetData = new TweetData(status).withMedia(resource);
        twitter.timelineOperations().updateStatus(tweetData);
    } catch (MissingAuthorizationException  ex) {
        throw new BadCredentialsException(ex.getMessage());
    }
}

当我尝试发布twitt时,我发现一个错误 java.lang.illegalargumentexception,未指定输入流。也许有人实现了这一点并且可以提供帮助?

0 个答案:

没有答案