A 100% pure Java library for the Twitter API with no extra dependency
npx @tessl/cli install tessl/maven-org-twitter4j--twitter4j-core@4.1.0Twitter4J Core is a 100% pure Java library that provides comprehensive access to the Twitter API v1.1 with no external dependencies. It offers a clean, object-oriented interface for all Twitter functionality including tweeting, timeline access, user management, search, streaming, and more.
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.1.2</version>
</dependency>implementation 'org.twitter4j:twitter4j-core:4.1.2'import org.twitter4j.*;
import org.twitter4j.v1.*;import org.twitter4j.*;
public class TwitterExample {
public static void main(String[] args) throws TwitterException {
// Configuration via properties file (twitter4j.properties)
Twitter twitter = Twitter.getInstance();
// Or programmatic configuration
Twitter twitter = Twitter.newBuilder()
.oAuthConsumer("consumer key", "consumer secret")
.oAuthAccessToken("access token", "access token secret")
.build();
// Access Twitter API v1.1
TwitterV1 v1 = twitter.v1();
// Post a tweet
Status status = v1.tweets().updateStatus("Hello Twitter API!");
System.out.println("Posted: " + status.getText());
// Get home timeline
ResponseList<Status> timeline = v1.timelines().getHomeTimeline();
for (Status tweet : timeline) {
System.out.println(tweet.getUser().getName() + ": " + tweet.getText());
}
}
}Twitter4J Core is built around several key architectural patterns:
Twitter.getInstance() and Twitter.newBuilder()Primary entry points and authentication management for Twitter API access.
interface Twitter {
static Twitter getInstance();
static TwitterBuilder newBuilder();
TwitterV1 v1();
}
class TwitterBuilder {
TwitterBuilder oAuthConsumer(String consumerKey, String consumerSecret);
TwitterBuilder oAuthAccessToken(String accessToken, String accessTokenSecret);
TwitterBuilder oAuth2AccessToken(String oauth2AccessToken);
Twitter build();
}Core Configuration and Authentication
Complete tweet lifecycle management including posting, retrieving, deleting, and retweeting.
interface TweetsResources {
Status updateStatus(String status) throws TwitterException;
Status updateStatus(StatusUpdate statusUpdate) throws TwitterException;
Status destroyStatus(long statusId) throws TwitterException;
Status retweetStatus(long statusId) throws TwitterException;
Status showStatus(long id) throws TwitterException;
}Access to various Twitter timelines including home timeline, user timeline, and mentions.
interface TimelinesResources {
ResponseList<Status> getHomeTimeline() throws TwitterException;
ResponseList<Status> getHomeTimeline(Paging paging) throws TwitterException;
ResponseList<Status> getUserTimeline() throws TwitterException;
ResponseList<Status> getUserTimeline(String screenName, Paging paging) throws TwitterException;
ResponseList<Status> getMentionsTimeline() throws TwitterException;
}User profile access, lookup, search, and relationship management.
interface UsersResources {
User showUser(String screenName) throws TwitterException;
User showUser(long userId) throws TwitterException;
ResponseList<User> searchUsers(String query, int page) throws TwitterException;
ResponseList<User> lookupUsers(String[] screenNames) throws TwitterException;
ResponseList<User> lookupUsers(long[] ids) throws TwitterException;
}
interface FriendsFollowersResources {
User createFriendship(String screenName) throws TwitterException;
User destroyFriendship(String screenName) throws TwitterException;
IDs getFriendsIDs(String screenName, long cursor) throws TwitterException;
IDs getFollowersIDs(String screenName, long cursor) throws TwitterException;
}Comprehensive search capabilities for tweets, users, and trending topics.
interface SearchResource {
QueryResult search(Query query) throws TwitterException;
}
interface TrendsResources {
Trends getPlaceTrends(int woeid) throws TwitterException;
ResponseList<Location> getAvailableTrends() throws TwitterException;
ResponseList<Location> getClosestTrends(GeoLocation location) throws TwitterException;
}Private messaging functionality for sending and receiving direct messages.
interface DirectMessagesResources {
DirectMessage sendDirectMessage(String screenName, String text) throws TwitterException;
DirectMessage sendDirectMessage(long userId, String text) throws TwitterException;
ResponseList<DirectMessage> getDirectMessages() throws TwitterException;
ResponseList<DirectMessage> getSentDirectMessages() throws TwitterException;
DirectMessage destroyDirectMessage(long id) throws TwitterException;
}Twitter list creation, management, and member operations.
interface ListsResources {
UserList createUserList(String listName, boolean isPublicList, String description) throws TwitterException;
UserList updateUserList(long listId, String newListName, boolean isPublicList, String newDescription) throws TwitterException;
ResponseList<UserList> getUserLists(String screenName) throws TwitterException;
ResponseList<Status> getUserListStatuses(long listId, Paging paging) throws TwitterException;
UserList addUserListMember(long listId, long userId) throws TwitterException;
UserList deleteUserListMember(long listId, long userId) throws TwitterException;
}Managing tweet favorites (likes) and retrieving favorite lists.
interface FavoritesResources {
Status createFavorite(long id) throws TwitterException;
Status destroyFavorite(long id) throws TwitterException;
ResponseList<Status> getFavorites() throws TwitterException;
ResponseList<Status> getFavorites(String screenName) throws TwitterException;
ResponseList<Status> getFavorites(String screenName, Paging paging) throws TwitterException;
}Real-time Twitter data streaming with customizable filters and listeners.
interface TwitterStream {
void addListener(StreamListener listener);
void filter(FilterQuery query);
void sample();
void firehose();
void user();
void cleanUp();
void shutdown();
}
interface StatusListener extends StreamListener {
void onStatus(Status status);
void onDeletionNotice(StatusDeletionNotice statusDeletionNotice);
void onTrackLimitationNotice(int numberOfLimitedStatuses);
void onScrubGeo(long userId, long upToStatusId);
void onStallWarning(StallWarning warning);
}Location-based functionality including places, geocoding, and geographic search.
interface PlacesGeoResources {
ResponseList<Place> searchPlaces(GeoQuery query) throws TwitterException;
ResponseList<Place> getSimilarPlaces(GeoLocation location, String name, String containedWithin, String streetAddress) throws TwitterException;
ResponseList<Place> reverseGeoCode(GeoQuery query) throws TwitterException;
Place getGeoDetails(String placeId) throws TwitterException;
}class TwitterException extends Exception {
int getErrorCode();
String getErrorMessage();
int getStatusCode();
boolean isCausedByNetworkIssue();
boolean isErrorMessageAvailable();
boolean exceededRateLimitation();
RateLimitStatus getRateLimitStatus();
}
interface TwitterResponse {
RateLimitStatus getRateLimitStatus();
int getAccessLevel();
}
interface RateLimitStatus {
int getLimit();
int getRemaining();
int getResetTimeInSeconds();
int getSecondsUntilReset();
}
class AccessToken {
AccessToken(String token, String tokenSecret);
String getToken();
String getTokenSecret();
long getUserId();
String getScreenName();
}
class OAuth2Token {
OAuth2Token(String tokenType, String accessToken);
String getTokenType();
String getAccessToken();
}