独自のホームページの中で、Twitterのタイムラインデータを取得したり、Twitterに投稿するにはTwitterとのプログラム間認証が必要になります。

その時使われるのが「OAuth認証」というものです。


ですがこの「OAuth認証」は認証を行う際ブラウザ間を行ったり来たりするする為、PCやスマートフォンからでしか行えません。


そうすると、モバイルサイトのみでサービスを行っている所とか困りますよね・・・。

そんなこんなで出てきたのが「xAuth認証」です。

言うなればxAuth認証はOAuth認証の簡易版で、ブラウザ間を行ったり来たりしません。
これならモバイルサイトでも行けるじゃん!!


ということでxAuth認証を試してみました。


まず最初に、Twitterにアプリケーションの登録を行います。
OAuth認証でももちろん同じことをします。


下記のURLから行います。
■Twitterアプリケーション登録申請
http://twitter.com/apps/new

こんな画面が出てきますね。

$IT(イケテル)社長の脳活100%!-Twitterアプリ登録申請


入力内容の説明はこちらを見て登録してみてください。


次にxAuth認証を利用するためには、Twitterに「Twitterとの認証するのに、xAuth使いたいんだけど許可してもらえますか」とメールで申請を行わなくてはなりません。

もちろん英語です。。。。

今回私のところで行っている、モバイルサービスに利用したいという旨を伝えたかったので下記の様な内容でメールしました。

--------------------------

Hello.
Hi. my name is YoshimiTakino.
My twitter id is @takinoyoshimi.

We developed a mobile site that was able to take communications with the fan that used the restaurant etc. and to exchange it for the coupon by collecting stamps this time.
Oauth was not able to be used because it was a site only for the cellular phone, and I thought feasible if it was xauth though I wanted to cooperate with twitter by the demand from the user.
Can it be asked for for the use permission of service?

Please append the screen of service to following URL, and it cannot be expected that an actual screen will not become a member, and refer to the image.
My best regards.

Application Name: ”ここにはアプリケーション名を記入”
App Image URL : ”ここではサイトのイメージ画像のURLを記入”
My account : @takinoyoshimi

Thank you.

--------------------------

数日後Twitterの方から返信があって、「xAuth許可したよ、詳しくはこのドキュメントを見てね」という内容でした。

ここまででひとまず準備オッケイです!


次に、実際にxAuth認証でTwitterにアクセスしてタイムラインを読み込んだり、投稿したりしてみました。


https://github.com/abraham/twitteroauth
上記URLのライブラリをダウンロードして、「OAuth.php」と「twitteroauth.php」をどこか別のところに抜き出します。


まあ、ひとまずテストなので。


xauth_testフォルダに、OAuth.phpとtwitteroauth.phpをコピーし

さらに

xauth_get.php
xauth_post.php

を同じフォルダ内に作成しました。


あとは

■xauth_get.php

<?php
require_once("twitteroauth.php");

$to = new TwitterOAuth("Consumer key","Consumer secret");

$to->getXAuthToken("twitter_ID","twitter_PW");

// home_timelineの取得。TwitterからXML形式が返ってくる
$req = $to->OAuthRequest("https://twitter.com/statuses/home_timeline.xml","GET",array("count"=>"5"));

// XML文字列をオブジェクトに代入する
$xml = simplexml_load_string($req);

// foreachで呟きの分だけループする
foreach($xml->status as $status){
$status_id = $status->id; // 呟きのステータスID
$text = $status->text; // 呟き
$user_id = $status->user->id; // ユーザーナンバー(数字の方は絶対に変わらない)
$screen_name = $status->user->screen_name; // ユーザーID(いわゆる普通のTwitterのID)
$name = $status->user->name; // ユーザーの名前(HNなど)
echo "
".$screen_name." / ".$name." この呟きのパーマリンク
\n".$text."

\n";
}
?>


■xauth_post.php

<?php
require_once("twitteroauth.php");

$to = new TwitterOAuth("Consumer key","Consumer secret");

$to->getXAuthToken("twitter_ID","twitter_PW");

// TwitterへPOSTする。パラメーターは配列に格納する
// in_reply_to_status_idを指定するのならば array("status"=>"@hogehoge reply","in_reply_to_status_id"=>"0000000000"); とする。
$req = $to->OAuthRequest("https://twitter.com/statuses/update.xml","POST",array("status"=>"phpプログラムからxAuth認証でTwitterにPOSTしてみるテスト"));
// TwitterへPOSTするときのパラメーターなど詳しい情報はTwitterのAPI仕様書を参照してください
?>


これで完了!

いやー、これは簡単で便利ですね!


*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
twitterフォローお願いします。
          → @takinoyoshimi
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


ペタしてね

読者登録してね