Posting Image Tweets On Twitter Using Java API

Posted By : Twinkle Rawat | 30-Nov-2021

api

Loading...

In this blog, We will be learning about how to post tweets with images on twitter using java api :

There are 2 ways to post on twitter :

1. By UsingTwitter REST API directly in Java

2. By UsingTwitter API Wrapper – Twitter4J

The Easiest way is to use Twitter 4J which we will be learning here.

Twitter4j :

It is an open source Java library, which helps us to integrate our java application with the Twitter services. It provides us different APIs to access twitter related informations.

By using Twitter4j Api we can :

  1. Post a tweet
  2. Get timeline of a user
  3. List of all the latest tweets
  4. Send and receive messages
  5. Search for tweets


Twitter4jMaven Dependency :

<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.4</version>
</dependency>


Twitter4j Configuration :
There are different ways forconfiguringtwitter4j API in ourapplication.We willcreate a file here with nametwitter4j.propertiesundersrc/main/resourcesfolder and put all the keysas shown below for the configuration :

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

Post Image Tweets :
Write yourmessage in the message Stringand then addone image. You cannot usepng format image,if you use png image then it will throw 400 Bad Request Exceptioncode from the HTTP response.

In the code, we will usearray for attaching image. You can also attach more than one image, according to your requirement.

CODE :

package com.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 PostingTweet{
public static void main(String[] args) throws TwitterException {
try {
Twitter twitter = TwitterFactory.getSingleton();
String statusMessage = <YOUR MESSAGE HERE>;
URL url = new URL(images.getPath());
BufferedImage img = ImageIO.read(url);
File file = new File(images.getS3BucketFileName());
ImageIO.write(img, "jpg", file);
long[] mediaIds = new long[1];
UploadedMedia media = twitter.uploadMedia(file);
mediaIds[0] = media.getMediaId();
StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
statusUpdate.setMediaIds(mediaIds);
Status status = twitter.updateStatus(statusUpdate);
log.info("Successfully Posted Tweet To Twitter");
file.delete();
return status.getId();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

We are a prominent ERP development company that provides end-to-end custom ERP software solutions for varying business needs. Our development team specializes in using advanced web frameworks like Angular, React, Node.js, and Django to build scalable business solutions. For more information, contact us at [email protected].