English

Tutorial: How to update Twitter status from Java


 Thursday, December 10, 2015 

How to interact with Twitter from a Java application using Twitter4J API
In this tutorial, I will explain how to interact with our Twitter account from a Java application using Twitter4J API.  To do this, we have to follow 3 steps:

1. Create Twitter App

We have to create an App in our Twitter account in order to be able to have access to authentication data from our Java application.

To do this, we have to go the url https://apps.twitter.com

We have to sign in with our Twitter account's user and password.

Once signed in, we're going to find the button "Create new App":




Clicking on the button will display a form where we can specify information for our new App:



The value for the Website field must have a valid URL format.  The website doesn't necessarily have to exist.

Once we complete specifying information, we must accept the Developer Agreement located at the bottom of the form, and create the App:



In the main screen of our App, we select the "Keys and Access Tokens" tab:



We scroll down to the bottom of the form and click on the "Create my access token" button:



This whole process will generate the 4 values needed to authenticate our Java application in Twitter:



These 4 values must be copied and pasted into our Java application code, as explained later.

We have to make sure that the access level of our App is "Read and write": 




2. Download and configure Twitter4J API in NetBeans IDE

We have to download Twitter4J API, which is the library that will provide us with all the resources needed to interact with our Twitter account from our Java application.


To do this, we have to go to the url http://twitter4j.org

In the Download section, we can find the ZIP file to download and unzip:




Now, we go to NetBeans IDE and configure Twitter4J API.


We right click on the Libraries section of our Java application and choose "Add JAR/Folder":



We have to choose the file "
twitter4j-core-4.0.4.jar" located in the "lib" folder of the downloaded package:



Now, we have the library ready to be used.


3. Java code

The following Java code updates our Twitter status by inserting a tweet into our TimeLine.

The 4 variables at the beginning of the code must be initialized with the values generated in the Twitter App creation process (step 1).


String CONSUMER_KEY = "GEKIvGRxG3RoyI1o3Vzle13eR";
String CONSUMER_SECRET = "PGehEF5E2jjeLrZtztjtjrvx1nk8iUAd1nrh7YlJODPKsgbGTF";
String ACCESS_TOKEN = "3222666444-aPeK2QEsbPF2E2lu2othYb7CvK9I6OnrTetAHGZ";
String ACCESS_TOKEN_SECRET = "S2YLul5b45yLRcGISOglScGXGki7yK8Bf2VGN9C0eU1Ry";

TwitterFactory tf = new TwitterFactory();
Twitter twitter = tf.getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET));

try {
     twitter.updateStatus("Tweet desde Java");
} catch (TwitterException e) {
     e.printStackTrace();
}



And, that's it!

I hope you find this useful.

Have a nice day.

No hay comentarios.:

Publicar un comentario