Awesome Open Source
Awesome Open Source

Twitter4J

#Twitter4J is a 100% pure Java library for the Twitter API with no extra dependency.

@t4j_news

Requirements

Java 8 or later

Dependency declaration

Add a dependency declaration to pom.xml, or build.gradle as follows:

Maven Central

Maven

<dependencies>
    <dependency>
        <groupId>org.twitter4j</groupId>
        <artifactId>twitter4j-core</artifactId>
        <version>4.1.2</version>
    </dependency>
</dependencies>

Gradle

dependencies {
    compile 'org.twitter4j:twitter4j-core:4.1.2'
}

Java modularity

requires org.twitter4j;

Getting started

Acquire an instance configured with twitter4j.properties, tweet "Hello Twitter API!".

twitter4j.properties

oauth.consumerKey=[consumer key]
oauth.consumerSecret=[consumer secret]
oauth.accessToken=[access token]
oauth.accessTokenSecret=[access token secret]

Main.java

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:

Main.java

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!");
  }
}

License

Apache License Version 2.0

Java CI with Gradle


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (411,723
Apache (16,287
Http Client (3,160
Java Library (2,159
Twitter Api (2,111