簡體   English   中英

使用Spring Framework Twitter API發布推文時圖像損壞

[英]Corrupted image when posting a tweet using spring framework twitter API

我正在嘗試使用Spring框架的twitter API從Java Tomcat服務器發布帶有嵌入式圖像的推文。 該圖像是在線托管(通過Amazon Cloudfront CDN)的JPG。 我嘗試在下面的代碼片段中使用updateTweet函數進行發布:

import org.springframework.social.twitter.api.TweetData;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.impl.TwitterTemplate;

// ...

public Tweet updateTweet(String accessToken, String accessTokenSecret, String tweetMessage, String imageUrl){
    TwitterTemplate twitter = new TwitterTemplate(twitterConsumerKey, twitterConsumerSecret, accessToken, accessTokenSecret);
    twitter.setRequestFactory(twitterHttpRequestFactory);

    TweetData tweetData = new TweetData(tweetMessage);
    try {
        UrlResource imageUrlResource = new UrlResource(imageUrl);
        logger.info("Trying to tweet image with url {} content length {}", imageUrl, imageUrlResource.contentLength());

        tweetData = tweetData.withMedia(imageUrlResource);
    } catch(MalformedURLException e) {
        logger.error("Malformed url for tweet image: {}", imageUrl);
    } catch(IOException e) {
        logger.error("IOException for tweet image {}\n{}", imageUrl, e);
    }
    return twitter.timelineOperations().updateStatus(tweetData);
}

該推文發布到我的用戶的時間軸上,並且確實包含尺寸正確的JPG圖像(640x640,如源圖像)-但是,實際的圖像數據已損壞! 這是一個損壞的圖像的示例,該圖像最終出現在我的Twitter時間軸上: https ://pbs.twimg.com/media/BVC5M5pCcAApIgP.jpg: large

我首先想到的是圖像數據被某種方式截斷了。 但是,我已經通過上面的代碼示例中的logger.info行確認,指向圖像的URLResource報告的內容長度與原始圖像的文件大小匹配。

我不確定為什么此代碼會將損壞的圖像數據發送到Twitter。 我已經搜索了一些工作示例,這些示例使用Spring框架中的TweetData.withMedia函數將圖像發布到Twitter,但我一直找不到。

您沒有指定使用哪個版本的spring-social-twitter ,但希望您會發現它有用。 根據您對TweetData類的使用,您的代碼建議您使用1.1.0.M4或更高版本。

我最近在我的應用中發現了同樣的問題。 在我從spring-social-twitter 1.0.3.RELEASE“升級”到1.1.0.M4之后的某個時間發生了。 我恢復到1.0.3.RELEASE之后,此問題已解決。 恢復到1.1.0.M3也可以解決該問題。

此錯誤記錄在Spring Social Twitter的Jira數據庫中:

https://jira.springsource.org/browse/SOCIALTW-71

如果恢復為較不易破解的版本,則必須停止使用TweetData 這是如何做:

TweetData tweetData = new TweetData(caption).withMedia(image);
timelineOperations.updateStatus(tweetData);

timelineOperations.updateStatus(caption, image);

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM