Tutorial on uploading image on twitter using twitter4j

This tutorial walks you through the steps to post a tweet or a tweet with a image on twitter.

First and foremost you should have a valid twitter app configured correctly to post on twitter.

In case you don’t have an app on twitter, follow the steps below to create a simple twitter app and how to configure it correctly.

Step 1.  Goto https://dev.twitter.com/apps Click on create new application button.

Then you will be presented the following page :

1_twit_create_app

Step 1

Fill in the details of your app.

Make sure you don’t leave the CallBack URL blank. A callback url is the one where your app will be redirected to from twitter; this is a test app so for time being you can specify something like eg: http://www.google.com . If you leave this field blank then your app wont be authorized.

Step 2. Now its time to configure your app. Click on the settings tab. Scroll down to the

Application type section and change access to read and write.

2_twit_settings_app

Step 2

Click on the update settings button at the bottom to reflect the changes. Ideally it takes 10 – 15 seconds for the settings to get updated so be patient.

Step 3. Download twitter4J library from the following url http://twitter4j.org/en/index.html and add it to your libs folder.

Note: The zip contains other additional jars. From these we need only twitter4j-core and

twitter4j-media support jars.

3_twit_libs_eclipse

Step 3

Step 4. Now lets get back to some coding. First you need to add the twitter consumer key and secret key to the Strings xml:

<string name = "twitter_consumer_key">YOUR_CONSUMER_KEY</string>
<string name= "twitter_consumer_secret">YOUR_CONSUMER_SECRET</string>
Step 4

Step 4

In our activity we have two buttons:

  • To post a tweet.

  • To post a tweet with image.

On button click we will check whether the app is already authorized if not we will authorize the app using a custom web view.

We will be using the custom webview to authorize the app so that once the app is authorized, you can finish the webview else you will be stuck with a common issue where the view remains opened even after the app is authorized.

Step 5

Step 5

 

The authorization logic is written in LoginActivity class.

Step 5. How to post a tweet/image on twitter ? Is this that tricky ? Lets have a look at it.

a. Post:

 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

 configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));

 configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));

 configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));

 configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));

 Configuration configuration = configurationBuilder.build();

 Twitter twitter = new TwitterFactory(configuration).getInstance();

 twitter.updateStatus(message);

b. Post with image:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));

configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));

configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));

configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));

Configuration configuration = configurationBuilder.build();

Twitter twitter = new TwitterFactory(configuration).getInstance();

StatusUpdate status = new StatusUpdate(message);

status.setMedia(file); // set the image to be uploaded here.

twitter.updateStatus(status);

As you can see posting is as simple as it can be. Further you can download live and functioning app.

Happy Coding Happy Learning 🙂

Advertisements

10 thoughts on “Tutorial on uploading image on twitter using twitter4j

  1. You made some nice points there. I did a search on the subject matter and found most individuals will agree with your site.

    Like

  2. Excellent web site. Lots of useful information here. I¡¦m sending it to some friends ans also sharing in delicious. And obviously, thanks for your effort!

    Like

  3. Hmm is anyone else encountering problems with the pictures on this blog loading?
    I’m trying to determine if its a problem on my end or if it’s the blog.
    Any feed-back would be greatly appreciated.

    Like

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