English | Japanese

Twitter4Jとは?

Twitter4J は TwitterAPI の Java ラッパです。
Twitter4J を使うと XML や HTTP に詳しくなくても容易に Twitter とインタラクトするアプリケーションを書くことが出来ます。

システム要件

OS: Java をサポートする Windows または Unix 系 OS
JVM: Java5 以降

使い方

twitter4j-1.1.5.jar をクラスパスに通して、好きなメソッドを呼び出してください。
Java が分かっていれば JavaDoc を見るのが早いです。
twitter4j.Twitter クラスが最初に見るべきクラスです。

コードサンプル

サンプルコードは src/twitter4j/examples/ 以下に配置されています。
それぞれ bin/className.cmd|sh で実行できます。

  1. スタテータスの更新
  2. Twitter.update() メソッドで"今なにをしているか"を更新することができます。
    詳しくは twitter4j.examples.Update.java をご覧ください。

        Twitter twitter = new Twitter(twitterID,twitterPassword);
        Status status = twitter.update(latestStatus);
        System.out.println("Successfully updated the status to [" + status.getText() + "].");
    

  3. タイムラインの取得
  4. Twitter.get****Timeline() メソッドで友達、パブリック、または指定ユーザの最近のタイムラインを返します。
    詳しくは twitter4j.examples.GetTimelines.java をご覧ください。

        Twitter twitter = new Twitter(twitterID,twitterPassword);
        statuses = twitter.getFriendsTimeline();
        System.out.println("Showing friends timeline.");
        for (Status status : statuses) {
            System.out.println(status.getUser().getName() + ":" +
                               status.getText());
        }
    

  5. ダイレクトメッセージの送受信
  6. Twitter.sendDirectMessage() / Twitter.getDirectMessages() メソッドでダイレクトメッセージの送受信ができます。
    このメッセージは送信した相手にしか見えません。
    詳しくは twitter4j.examples.Get/SendDirectMessage(s).java をご覧ください。

        Twitter twitter = new Twitter(senderID,senderPassword);
        sender.sendDirectMessage(recipientId,message);
        Twitter receiver = new Twitter(recipientId,recipientPassword);
        List messages = receiver.getDirectMessages();
        for (DirectMessage message : messages) {
            System.out.println("Sender:" + message.getSenderScreenName());
            System.out.println("Text:" + message.getText() + "\n");
        }
    

  7. 非同期API
  8. 非同期APIを使うと実際のメソッドコールの終了を待たずして処理を続行することができます。
    実際のメソッドコールは別のスレッドで行われ、処理の結果は TwitterListener インターフェースにて受信できます。
    非同期 API を使うには Twitter クラスの替わりに twitter4j.AsyncTwitter クラスを使い、***Async() メソッドを twitter4j.TwitterListener のインスタンスと共に呼び出します。
    詳しくは twitter4j.examples.AsyncUpdate.java をご覧ください。

        AsyncTwitter twitter = new AsyncTwitter(senderId,senderPassword);
        twitter.updateAsync(args[2], new TwitterAdapter() {
            @Override public void updated(Status status) {
            System.out.println("Successfully updated the status to [" +
                       status.getText() + "].");
            synchronized (lock) {
                lock.notify();
            }
            }
            @Override public void onException(TwitterException e, int method) {
            if (method == UPDATE) {
                e.printStackTrace();
                synchronized (lock) {
                lock.notify();
                }
            } else {
                synchronized (lock) {
                lock.notify();
                }
                throw new AssertionError("Should not happen");
            }
            }
        }
        );
    

ダウンロード

ダウンロード
twitter4j-1.1.5.zip

ソースコード

アーカイブにはソースコードが含まれています。
プロジェクトのリポジトリには以下の URL からアクセスできます。
http://yusuke.homeip.net/fisheye/browse/svn/twitter4j/trunk
または以下のように subversion クライアントを使って最新のソースコードをチェックアウトすることもできます。

svn checkout http://yusuke.homeip.net/svn/twitter4j/trunk twitter4j

Maven 統合

Maven を使っている場合、pom.xml に以下のように記載すればご自身のプロジェクトに twitter4j を簡単に統合できます。

   <dependencies>
<dependency>
<groupId>net.homeip.yusuke</groupId>
<artifactId>twitter4j</artifactId>
<version>[1.1,)</version>
</dependency>
...
</dependencies>

メーリングリスト

Twitter4J に関する質問、バグレポート、リクエストなどありましたら以下のメーリングリストにお気軽にどうぞ。
Google グループ
Twitter4J Jに参加
メール アドレス:
このグループにアクセス
バグ、ロードマップなどについては Jira に登録してあります。
Jira へ

ライセンス

Twitter4J は BSD スタイルライセンスに基づいてリリースされています。

Copyright (c) 2007-2008, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

価格

Twitter4J はどなたでも商標、非商用にかかわらず無償でご利用いただけます。
もし Twitter4J があなたの生活を豊かにしてくれると感じたならば、以下の PayPal バッジから寄付していただくことも可能です。

変更履歴

    Release Notes - Twitter4J - Version 1.1.5 new! 2/25/2009

    Bug

    • [TFJ-76] - remove sun.misc.BASE64Encoder dependency

    Improvement

    • [TFJ-74] - twitter4j.http.Response ignores SAXException, makes it difficult to troubleshoot
    Release Notes - Twitter4J - Version 1.1.4 new! 1/12/2008

    Bug

    • [TFJ-70] - getting a protected user profile causes a NullPointerException

    Improvement

    • [TFJ-69] - some async methods are declared to throw TwitterException mistakenly

    New Feature

    • [TFJ-48] - support update_delivery_device method
    • [TFJ-49] - support update_profile_colors method
    • [TFJ-50] - support update_profile_image method
    • [TFJ-51] - support update_profile_background_image method
    • [TFJ-52] - support exists method
    • [TFJ-53] - support rate_limit_status method
    • Release Notes - Twitter4J - Version 1.1.3

      Bug

      • [TFJ-67] - verifyCredentials() returns always false since 10/12/2008

      New Feature

      • [TFJ-33] - ability to get the authenticated user

      Task

      • [TFJ-65] - methods don't have to be final
      Release Notes - Twitter4J - Version 1.1.2

      Bug

      • [TFJ-61] - AsyncTwitter#createFavoriteAsync(), destoryStatusAsync(), destroyFavoriteAsync() should take long instead of int
      • [TFJ-64] - AsyncTwitter#show(long) fails with ClassCastException

      Improvement

      • [TFJ-63] - typo: detroy -> destory
    • Release Notes - Twitter4J - Version 1.1.1
    • Bug

      • [TFJ-54] - Show method should take long instead of int
      • [TFJ-55] - Show method shouldn't require userid and password
      • [TFJ-56] - TwitterResponse from AsyncTwitter is not Serializable
      • [TFJ-57] - Date is encoded twice, and therefore "since" doesn't work
      • [TFJ-60] - hashCode and equals are not properly implemented

      Improvement

      • [TFJ-47] - implement toString()
    • バージョン 1.1.0
    • 修正されたバグ

      • [TFJ-44] - #document: null] is unnecessarily printed to the System.out

      改善

      • [TFJ-31] - dereference DOM objects to reduce memory footprint
      • [TFJ-34] - Status#getCreatedAt(), UserWithStatus.getCreatedAt() and DirectMessage.getCreatedAt() should return java.util.Date
      • [TFJ-41] - Status.getId(), UserWithStatus.getStatusId() should return long instead of int
      • [TFJ-43] - remove unsupported methods

      新機能

      • [TFJ-45] - getFollowers() should accept id and/or page parameter
    • バージョン 1.0.6
    • 修正されたバグ

      • [TFJ-38] - getDirectMessage(int since) が 500 Internal Server Error で失敗する

      改善

      • [TFJ-37] - Unexpected root node name:nilclasses. Expected:nil-classes
      • [TFJ-42] - maximum number of count is now 200
    • バージョン 1.0.5

      修正されたバグ

      • [TFJ-36] - いくつかのメソッドが 400 コードで失敗した

      新機能

      • [TFJ-35] - status を削除するメソッドを追加
    • バージョン 1.0.4
    • 改善

      • [TFJ-22] - pom.xml が source jar を生成しない
      • [TFJ-27] - "source", "truncated", "in_reply_to_status_id", "in_reply_to_user_id", "favorited" を Status に, "followers_count" を User に追加

      新機能

      • [TFJ-21] - POST メソッドを強制する機能
      • [TFJ-24] - 2008/4/29に導入された新APIのサポート
      • [TFJ-25] - ユーザIDとパスワードを変更する機能
      • [TFJ-26] - リクエスト、レスポンスをダンプする機能
    • バージョン 1.0.3
    • 新機能

      • [TFJ-16] - follow / leave メソッドのサポート
      • [TFJ-17] - getFriends()メソッドでページ番号を指定できるように
      • [TFJ-18] - アカウントメソッドのサポート
      • [TFJ-19] - お気に入りメソッドのサポート
    • バージョン 1.0.2
    • 修正されたバグ

      • [TFJ-13] - twitter4j jar の署名に失敗する
      • [TFJ-14] - getPublicTimeline(sinceId) の引数は整数であるべき

      改善

      • [TFJ-9] - Commons-Codec への依存の除去

      新機能

      • [TFJ-8] - "source" パラメータとリクエストヘッダを設定できるように
      • [TFJ-10] - ダイレクトメッセージの"sender" と "recipient" 要素のサポート
      • [TFJ-11] - "sent" APIのサポート
      • [TFJ-12] - ページングのサポート
      • [TFJ-15] - getDirectMessages() に sinse_id を渡せるように
    • バージョン 1.0.1
    • 修正されたバグ

      • [TFJ-2] - Twitter#getUserDetail() で NullPointerException が発生する
      • [TFJ-3] - Twitter#getFriends() で取得したUserインスタンスのうちいくつかのメソッドで NullPointerException が発生する
      • [TFJ-4] - getDirectMessages() で取得したダイレクトメッセージの件数が0件だと TwitterException が発生する
      • [TFJ-5] - Twitter#getReplies() で TwitterException が発生する
      • [TFJ-6] - Twitterサービスのメンテナンス時に NullPointerException が発生する

      新機能

      • [TFJ-1] - ダイレクトメッセージ削除APIのサポート
    • バージョン 1.0.0
    • 初期バージョンリリース