Twitter4J

Additional

Language
Java
Version
4.0.7 (Aug 18, 2018)
Created
Apr 24, 2009
Updated
Jul 25, 2023
Owner
Twitter4J
Contributors
Vadim Spivak (vadims)
Javier Pérez Pacheco (javipacheco)
Pieter Meiresone (MPieter)
Halo Master (linkerlin)
Steve Lhomme (robUx4)
Daniel Huckaby (DHuckaby)
Tomasz Nurkiewicz (nurkiewicz)
Roberto Estrada (RobertoEstrada)
TAKEUCHI Hiroaki (takke)
Jenny Williamson (jloomis)
Q P Liu (qpliu)
Gonçalo Silva (goncalossilva)
kenji yoshida (xuwei-k)
Emmanuel Bourg (ebourg)
Tsung Wu (ionull)
Hendy Irawan (ceefour)
Chris Nix (BryceCicada)
Christian Brüggemann (cbruegg)
Show all (127)127
Activity
Badge
Generate
Download
Source code

X4J

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

Requirements

Java 8 or later

Dependency declaration

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

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