How to post image tweets to Twitter using Twitter4j Java API

In this example I am going to discuss on how to post image tweets to Twitter using Twitter4j Java API. Twitter4j is an unofficial Java library and using this you can easily integrate Java application with Twitter service.

Using image tweets I want to mean that I want to post text as well as image with this tweet. As many images as you wish to add to your tweet. Basically an array is defined to hold the images along with your text.

Related Posts:

Prerequisites

Eclipse 2020-06, Java at least 1.8, twitter4j 4.0.7

Project Setup

I am going to create gradle based project in Eclipse. The name of the project is java-twitter4j-image-tweets.

The build.gradle script is given below:

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
	implementation 'org.twitter4j:twitter4j-core:4.0.7'
}

Twitter4j Configuration

There are number of ways you can configure twitter4j API for your Java application. But here I am going to use properties file in the claspath.

Create a file called twitter4j.properties under src/main/resources folder and put the similar condigurations as shown below.

debug=true
oauth.consumerKey=<consumer key>
oauth.consumerSecret=<consumer secret>
oauth.accessToken=<access token>
oauth.accessTokenSecret=<sccess token secret>

You can get those details from Twitter Developers under the menu Keys and Tokens.

Post Image Tweets

Now we will see how to post image tweets or status on twitter. The tweet message is simple “My Picture” and I am adding one image. The type of the image cannot be png and if you use png image then you will get 400 Bad Request status code from the HTTP response.

Here I am using array for attaching image. You may want to attach more than one image.

package com.roytuts.java.twitter4j.tweets;

import java.io.File;

import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.UploadedMedia;

public class TweetLinkWithImage {

	public static void main(String[] args) throws TwitterException {
		Twitter twitter = TwitterFactory.getSingleton();

		// post a tweet link with image
		String statusMessage = "My Picture";
	    File imagefile = new File("soumitra.jpg");

	    long[] mediaIds = new long[1];
	    UploadedMedia media = twitter.uploadMedia(imagefile);
	    mediaIds[0] = media.getMediaId();

	    StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
	    statusUpdate.setMediaIds(mediaIds);
	    
		Status status = twitter.updateStatus(statusUpdate);
		System.out.println("Successfully updated the status to [" + status.getText() + "].");
	}

}

Testing the Application

By executing the above code I got the below output as shown in the below image:

Successfully updated the status to [My Picture https://t.co/K7iMWqqxNo].

On Twitter home page I got below image:

That’s all.

Source Code

Download

Thanks for reading.

Leave a Comment