2
votes

I am trying to publish a tweet with an embedded image from a Java Tomcat server using the Spring framework's twitter API. The image is a JPG hosted online (via Amazon Cloudfront CDN). I try to post using the updateTweet function in the code snippet below:

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);
}

The tweet posts to my user's timeline, and does include a JPG image with the correct dimensions (640x640, like the source image) - however, the actual image data is corrupted! Here is an example of the corrupted image that ends up on my twitter timeline: https://pbs.twimg.com/media/BVC5M5pCcAApIgP.jpg:large

My first thought was that the image data was being truncated somehow. However, I've confirmed via the logger.info line in the code sample above that the URLResource pointing at the image reports a content-length matching the filesize of the original image.

I am unsure why this code is sending corrupted image data to twitter. I have searched for working examples that post images to twitter using the TweetData.withMedia function from Spring's framework, but I haven't been able to find one.

1

1 Answers

0
votes

You did not specify which version of spring-social-twitter you are using, but hopefully you will find this useful. Based on your usage of the TweetData class, your code suggests you are using 1.1.0.M4 or later.

I recently discovered the same problem with my app. It happened sometime after I "upgraded" from spring-social-twitter 1.0.3.RELEASE to 1.1.0.M4. After I reverted back to 1.0.3.RELEASE, the problem was fixed. Reverting back to 1.1.0.M3 also fixes the problem.

This bug is documented in Spring Social Twitter's Jira database:

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

If you revert to a less broken version, you'll have to discontinue the use of TweetData. Here's how:

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

becomes

timelineOperations.updateStatus(caption, image);

Good luck!