Using Twitter API With Java

Using Twitter API With Java explains step by step details of Configuring Twitter API With Java.

How to configure Twitter API With Java?

The following are some of the possibilities that you have, in order to integrate Twitter service

On this Twitter4J Example, I am showing How to use twitter with Twitter4J

Twitter4J is a free open source API (Twitter4J is not maintained by Twitter service) By using this API you can easily integrate Twitter service with your Java application.

Required Libraries

You need to download

  1. Eclipse 4.2
  2. Twitter4J

First you need to create a twitter application, for this you need to use following link

https://dev.twitter.com/apps

You need to login with twitter credentials, a twitter account is mandatory (please see the below screenshot)

Using Twitter API With Java

 

After the registration, you will get two types of credentials

  1. Consumer key
  2. Consumer secret
  1. Access token
  2. Access token secret

You can see Consumer key & Access token generation on below screen shot

Using Twitter API With Java

 

We required both types credentials for interacting the Twitter service

Please find an example and it's output about how Twitter4j Connecting with Twitter service.

package test.twitter;

import twitter4j.Paging;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

//Twitter API With Java Example
public class Twitter4jExample {
 
public static void main(String args[]) {
   
Twitter twitter = new TwitterFactory().getInstance();
   
// Twitter Consumer key & Consumer Secret
   
twitter.setOAuthConsumer("l5HHK9Zif6cbQLVTm2lzTQ", "3Jp8X9B78MijlxgFXrp76S5mT2QkqK1iOytZ8z1sc");
   
// Twitter Access token & Access token Secret
   
twitter.setOAuthAccessToken(new AccessToken("371118809-g8MSGnFNZVe1ZDHynOQF4x83vIsCkJAinI4EIhRW",
   
"MEeLoM0rC90gucCdVpdeuacfYvWcLIhDpaS8XDlr4Ts"));
   
try {
     
// Getting Twitter Timeline using Twitter4j API
     
ResponseList statusReponseList = twitter.getUserTimeline(new Paging(1, 5));
     
for (Status status : statusReponseList) {
       
System.out.println(status.getText());
     
}
     
// Post a Tweet using Twitter4j API
     
Status status = twitter.updateStatus("Hello");
      System.out.println
("Successfully updated the status to [" + status.getText() + "].");
   
} catch (Exception e) {
    }
  }
}
Output
Change CXF Character Encoding http://t.co/xrPeY5O0
Getting IP Address Using CXF http://t.co/IuKtDRkh
GZIP Using CXF http://t.co/XAOObwAE
Apache CXF Logging http://t.co/UH0dPhiD
RESTful Java Client Using HttpClient http://t.co/j89o1IWY

 











Your email address will not be published. Required fields are marked *