Using update_with_media with Twitter4J

Twitter recently introduced a new API called “update_with_media”. It allows users to post pictures and other media directly to Twitter, rather than using a third-party platform like TwitPic.com.

The API is documented here:

https://dev.twitter.com/docs/api/1/post/statuses/update_with_media

Twitter4J is an excellent library for integrating Twitter support into Java applications, including those running on Android. Twitter4J is available here:

http://twitter4j.org/en/index.html

I was recently using Twitter4J 2.2.5, and found myself wanting to use the new Twitter API to post some pictures. There didn’t seem to be an available method in the library, though, that made use of the newly published Twitter endpoint, and a quick search of the codebase for “update_with_media” turned up nothing, so I decided to modify Twitter4J to support “update_with_media”. The changes are actually relatively painless, and are documented below:

1. Find the “StatusMethods” class in the “twitter4j.api” package, and add the following interface”:

Status updateStatusWithFileMedia(String status,File media) throws TwitterException;

We’re going to be uploading the media (a JPG image, in this case) directly from its associated File object on the filesystem.

2. Open up the “TwitterImpl” object in the “twitter4j” package, and implement interface as follows:

    public Status updateStatusWithFileMedia(String status, File media) 
        throws TwitterException {
        // check authorization
    	ensureAuthorizationEnabled();
    	
    	// construct the custom request
    	String url = "https://upload.twitter.com/1/statuses/update_with_media.json";
    	HttpParameter p1 = new HttpParameter("status",status);
    	HttpParameter p2 = new HttpParameter("media[]",media);
    	HttpParameter[] arr = { p1, p2 };
    	
    	// make call to factory method
    	return factory.createStatus(post(url,arr));
}

and that’s it!

We can now upload media files (i.e., JPG files) directly to Twitter using the following sample code:

import java.io.File;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class Driver {
	public static void main(String[] args) {
                // configure your twitter access tokens
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setOAuthConsumerKey("XXX");
		cb.setOAuthConsumerSecret("XXX");
		cb.setOAuthAccessToken("XXX");
		cb.setOAuthAccessTokenSecret("XXX");
	
                // grab a factory instance and post to twitter!	
		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitter = tf.getInstance();
		try {
			twitter.updateStatusWithFileMedia("Pictures of #saturn",new File("Saturn1.jpg"));
		} catch ( Exception ex ) {
			ex.printStackTrace();
		}
	}
}

2 thoughts on “Using update_with_media with Twitter4J

    • The best way to “change” a .class file is simply to recompile the corresponding .java file using javac.

      Bear in mind also that it’s been a while since I posted this.

      The more recent versions of Twitter4J I think already incorporate this change (or so I’ve been told, I haven’t followed up to verify).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s