Created
April 27, 2014 16:50
-
-
Save shunsuke0125/11350239 to your computer and use it in GitHub Desktop.
Twitter4j を用いたシンプルな実装例 (2014/04/28 01:46:00 動作確認済み)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.util.List; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.conf.ConfigurationBuilder; | |
/** | |
* <h1>Twitter4jのサンプルプログラム</h1> | |
* | |
* <p>本プログラムを実行するには<a href="http://twitter4j.org/ja/">こちら</a>にある<strong>twitter4j-core-4.0.1.jar</strong>を参照ライブラリーに追加してください.</p> | |
* <p>更にTwitterのAPIを使用するために必要なキーを事前に入手する必要があります. 入手するには以下の手順を踏みます.</p> | |
* <ol> | |
* <li><a href="https://apps.twitter.com/">https://apps.twitter.com/</a> にアクセスしてTwitterアカウントでログインします.</li> | |
* <li>[Create New App]ボタンをクリックします.</li> | |
* <li>Create an application画面の項目を入力します.</li> | |
* <ul> | |
* <li>Name: 他のアプリケーション名に重複しない名称を入力してください.</li> | |
* <li>Description: アプリケーションの説明です.</li> | |
* <li>Website: サイトのURLです. ローカル環境でテスト用に使用するには<em>http://127.0.0.1/</em>を設定します.</li> | |
* <li>Callback URL: サイト上でTwitter認証の際に使うコールバックのURLです. BOTやBatchプログラムの場合は設定不要です.</li> | |
* <li>Developer Rules of the Road: 規約です. チェックを入れてください.</li> | |
* </ul> | |
* <li>APIの管理画面が表示されるので[Details]タブの一番下にある[Create My Access Token]ボタンをクリックします.</li> | |
* <li>アプリケーション名の右側にある[Test OAuth]ボタンをクリックします.</li> | |
* <li>表示される[Consumer key][Consumer secret][Access token][Access token secret]を以下の定数値に入力します.</li> | |
* </ol> | |
* <p>初期状態だと読み取りのみ有効(Read only)になっています. アプリケーション内で投稿やDMの管理をする場合は[Permission]タグから変更してください.</p> | |
* <hr> | |
* <p>Twitter4jを用いてWebシステム内でTwitterアカウントによるサインインを実装するには<a href="http://twitter4j.org/ja/code-examples.html">公式サイト</a>の「8. Twitterアカウントによるサインイン」を参照してください.</p> | |
* <p>シンプルな実装例は<a href="https://github.com/yusuke/sign-in-with-twitter">https://github.com/yusuke/sign-in-with-twitter</a>を参照してください.</p> | |
* | |
* @author shunsuke ishida | |
* | |
*/ | |
public class Twitter4jSampleMain { | |
/** OAuth Settingsの値 */ | |
private static final String oAuthConsumerKey = "*************************"; | |
private static final String oAuthConsumerSecret = "**************************************************"; | |
private static final String oAuthAccessToken = "**************************************************"; | |
private static final String oAuthAccessTokenSecret = "*********************************************"; | |
/** | |
* <h1>メインメソッド</h1> | |
* <p>定数であるAPIキーをもとにTwitterオブジェクトを生成し、タイムラインを標準出力します.</p> | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// Twitterオブジェクトを取得します. | |
Twitter tw = getTwitter(); | |
// タイムラインを取得して標準出力します. | |
try { | |
List<Status> statusList = tw.getHomeTimeline(); | |
System.out.println("Showing home timeline."); | |
for (Status status : statusList) { | |
System.out.println(status.getUser().getName() + ":" + status.getText()); | |
} | |
} catch (TwitterException te) { | |
System.out.println("Exception: " + te); | |
} | |
} | |
/** | |
* Twitterオブジェクトを生成して返却します. | |
* | |
* @return Twitterオブジェクト | |
*/ | |
private static Twitter getTwitter() { | |
ConfigurationBuilder cb = new ConfigurationBuilder(); | |
cb.setDebugEnabled(true); | |
cb.setOAuthConsumerKey(oAuthConsumerKey); | |
cb.setOAuthConsumerSecret(oAuthConsumerSecret); | |
cb.setOAuthAccessToken(oAuthAccessToken); | |
cb.setOAuthAccessTokenSecret(oAuthAccessTokenSecret); | |
TwitterFactory tf = new TwitterFactory(cb.build()); | |
return tf.getInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment