Firstly, you need to create an app in https://apps.twitter.com using a developer account in Twitter. During this process, if you are doing this for first time, you may need to sign up in this & then create an app.
To further explain this, actually Twitter provides OAuth credentials to this app, using those OAuth credentials, your app can successfully login to tied up Twitter account, then it can post, read & any other such Twitter operations can be done by app on behalf of the actual Twitter account. It works like, your app acts as client app with Twitter as OAuth provider. Just like any other OAuth Client with a OAuth Provider. A typical OAuth flow.
After creating an app in above mentioned link, you need to set the permissions for your app - if you want only read the tweets - A read-only permission would be enough. Otherwise if you want to tweet (post a tweet) from you app, you need to give write permissions as well.
The same link apps.twitter.com gives you these four important OAuth related information, which you will read & configure in you application (java application).
OAuth Consumer Key: <Some alphanumeric key>
[Recognises you app as a consumer]
OAuth Consumer Secret: <Some alphanumeric key>
[represents your app secret]
OAuth Access Token: <Some alphanumeric key>
[Recognises Access Token - valid for some time - only - you can revoke this token & regenerate - whenever you like to]
OAuth Access Token Secret: <Some alphanumeric key>
[Recognises Access Token Secret]
NOTE: Keep these credentials in secret configuration locations like VAULT.
Dependencies Required: twitter4j-stream
Gradle:
implementation group: 'org.twitter4j', name: 'twitter4j-stream', version: '4.0.7'Add the above dependencies in your build.gradle (or if your are using maven, pls form the maven config as similar to the above groupid & artefact)-----------------------------------------------------------------------------package com.basic.twitter;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class TwitterDemo {
public static void main(String[] args) throws TwitterException {
Twitter twitter = getTwitterInstance();
readTweets(twitter);
String message= "I am writing this tweet from my Java IDE using twitter4j-stream";
sendTweet(twitter, message);
}
private static void sendTweet(Twitter twitter, String message)throws TwitterException {
Status status = twitter.updateStatus(message);
System.out.println(status.getRetweetCount()); //checking any retweet count
}
private static void readTweets(Twitter twitter) throws TwitterException {
ResponseList<Status> messages = twitter.getHomeTimeline();
messages.stream().map(m -> m.getText()).forEach(message -> {
System.out.println(message);
System.out.println("--------------------------------\n");
});
}
private static Twitter getTwitterInstance() {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder
.setOAuthConsumerKey("<<Your OAuth Consumer Key>>")
.setOAuthConsumerSecret("<<Your OAuth Consumer Secret>>")
.setOAuthAccessToken("<<Your OAuth Access Token>>")
.setOAuthAccessTokenSecret("<<Your OAuth Token Secret>>");
TwitterFactory twitterFactory= new TwitterFactory(configurationBuilder.build());
return twitterFactory.getInstance();
}
}


