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 で実行できます。
- スタテータスの更新
- タイムラインの取得
- ダイレクトメッセージの送受信
- 非同期API
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() + "].");
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()); }
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); Listmessages = receiver.getDirectMessages(); for (DirectMessage message : messages) { System.out.println("Sender:" + message.getSenderScreenName()); System.out.println("Text:" + message.getText() + "\n"); }
非同期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 に関する質問、バグレポート、リクエストなどありましたら以下のメーリングリストにお気軽にどうぞ。
![]() |
Twitter4J Jに参加 |
このグループにアクセス |
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 バッジから寄付していただくことも可能です。
変更履歴
- [TFJ-76] - remove sun.misc.BASE64Encoder dependency
- [TFJ-74] - twitter4j.http.Response ignores SAXException, makes it difficult to troubleshoot
- [TFJ-70] - getting a protected user profile causes a NullPointerException
- [TFJ-69] - some async methods are declared to throw TwitterException mistakenly
- [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
- [TFJ-67] - verifyCredentials() returns always false since 10/12/2008
- [TFJ-33] - ability to get the authenticated user
- [TFJ-65] - methods don't have to be final
- [TFJ-61] - AsyncTwitter#createFavoriteAsync(), destoryStatusAsync(), destroyFavoriteAsync() should take long instead of int
- [TFJ-64] - AsyncTwitter#show(long) fails with ClassCastException
- [TFJ-63] - typo: detroy -> destory
- Release Notes - Twitter4J - Version 1.1.1
- [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
- [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-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 初期バージョンリリース