Twitter REST APIs in Java

Twitter is undoubtedly one of the most successful examples of social networking to appear on the World Wide Web. The REST APIs provide programmatic access to read and write Twitter data.

Rest API

  • You can call Twitter REST APIs to read and write Twitter data.
  • The REST API identifies Twitter applications and users using OAuth.
  • Responses are available in JSON.

There are many REST APIs available, you can see it here https://dev.twitter.com/rest/public. Note: Most of the twitter API are rate limited, it means you can call particular API for certain number of times within each 15 minute time. Different API are having different rate limit, for example https://api.twitter.com/1.1/statuses/home_timeline.json API is having API rate limit of 15 and it means you can call this API only 15 times within each 15 minute period.

OAuth Authentication

As of version 1.1, the Twitter API now requires OAuth authentication, either application-only authentication or application-user authentication. When you begin to access the Twitter API on behalf of a user, your user will be directed to Twitter to authorize your application. Twitter will return tokens which do not expire until the user revokes them. You will use these tokens to authenticate your calls on behalf of this user.

Steps to use the Rest API in Java:

    1. Register your application on Twitter
    2. Create Your Access Token for OAuth
    3. Use twitter4j library to use Rest API

Register your application on Twitter:

Following are the steps to register your application on twitter:

Create Your Access Token for OAuth:

Once you have registered your application successfully, you can see the details by signing in at https://apps.twitter.com/ .
You have to give the access to your Twitter Account to use this Application. To do this, click the Create my Access Token.
Depending on your need, select any one permission from Read Only, Read and Write and Read, Write and Access direct messages.
In order to access the Twitter, you need the four keys such as Consumer Key, Consumer Secret, Access token and Access Token Secret.

Note:whenever you will change the permission, you must regenerate keys and Access Tokens.
You need to copy Consumer Key, Consumer Secret, Access token and Access Token Secret into your application.
Use twitter4j library to call Rest API

Twitter4J is an unofficial Java library for the Twitter API.
With Twitter4J, you can easily integrate your Java application with the Twitter service.

System requirement:JVM 5 or later
Configuration:

Just add twitter4j-core-4.0.4.jar to your application classpath.
You can download it from here http://twitter4j.org/archive/twitter4j-4.0.4.zip .

There are a number of properties available for configuring Twitter4J:

  • oauth.consumerKey
  • oauth.consumerSecret
  • oauth.accessToken
  • oauth.accessTokenSecret

You can specify the properties via twitter4j.properties file, ConfigurationBuilder class or System Property. I chose to add a twitter4.properties file in the classpath. Under src/
Copy your Consumer Key, Consumer Secret, Access Token and Access Token Secret into file named twitter4j.properties and add this file to source path. Do not share your OAuth keys with anyone.

Example:  Get users home timeline by calling statuses/home_timeline Rest API

Returns a collection of the most recent Tweets and retweets posted by the authenticating user and the users they follow, for this we need to call https://api.twitter.com/1.1/statuses/home_timeline.json  API. twitter4j provides getHomeTimeline() method to call this API.

The code is fairly simple. I have just created a Twitter instance from TwitterFactory, and getHomeTimeline method is called.

Reference: