#Twitter4J is a 100% pure Java library for the Twitter API with no extra dependency.
Java 8 or later
Add a dependency declaration to pom.xml, or build.gradle as follows:
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
dependencies {
compile 'org.twitter4j:twitter4j-core:4.1.2'
}
requires org.twitter4j;
Acquire an instance configured with twitter4j.properties, tweet "Hello Twitter API!".
oauth.consumerKey=[consumer key]
oauth.consumerSecret=[consumer secret]
oauth.accessToken=[access token]
oauth.accessTokenSecret=[access token secret]
import org.twitter4j.*;
public class Main {
public static void main(String... args){
Twitter twitter = Twitter.getInstance();
twitter.v1().tweets().updateStatus("Hello Twitter API!");
}
}
v1() returns TwitterV1 interface which provides various Twitter API V1.1 API resources. tweets() returns TweetsResources.
You can also get a builder object from newBuilder() method to configure the instance with code:
import org.twitter4j.*;
public class Main {
public static void main(String... args){
var twitter = Twitter.newBuilder()
.oAuthConsumer("consumer key", "consumer secret")
.oAuthAccessToken("access token", "access token secret")
.build();
twitter.v1().tweets().updateStatus("Hello Twitter API!");
}
}
Apache License Version 2.0