Using Twitter API With Java

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

 





Previous 1 2








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