(2015-01-20加筆)本記事の内容は古くなっていて、ここに記した隠し Twitter API が廃止・変更されている。

Twitter の標準機能として、Tweet を埋め込む機能がある。 ツイートの詳細画面で「このツイートをサイトに埋め込む」をクリックして、HTML のコードをコピペするだけ、と非常に簡単につかえる。

HTMLコード

これはフォローボタンや必要あれば画像などを含んだリッチな表示がなされるが、 http://platform.twitter.com/widgets.js (埋め込んだサイトが https ならば https://platform.twitter.com/widgets.js になる。ローカル(file)ならば呼び出しに失敗するはず。) による動的生成でつくられている。 ただ、JavaScript が動かなくともツイートのテキストを含めるような仕組みになっている。 以下にソースコードの例を示す(読みやすさのためにインデントと改行を挿入してある)。

<blockquote class="twitter-tweet">
  <p>
    プライバシー等の問題を抜きにしてもビッグデータのビジネスは日本(特に大手企業)からは生まれなさそうだ。何らかのサービスを提供する手段であるはずが目的になってしまっている。 
    <a href="https://twitter.com/search/%23ビッグデータ">
      #ビッグデータ
    </a>
    <a href="http://t.co/yl6s6sma" title="http://twitter.com/cat_in_136/status/176199889313734657/photo/1">
      twitter.com/cat_in_136/sta…
    </a>
  </p>
  &mdash; cat_in_136 (@cat_in_136) 
  <a href="https://twitter.com/cat_in_136/status/176199889313734657" data-datetime="2012-03-04T06:58:22+00:00">
    March 4, 2012
  </a>
</blockquote>
<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

なお、JavaScript が無効にした場合やソースコードを貼りつけた場合、Developer Display Requirementsの下記に違反することとなってしまうが、Twitter 標準機能がそうなっているんだからしょうがない。

  • 1.d. The avatar, name, and @username must all link to the user’s Twitter profile
    • name と @username がリンクされていない。(加えて、非モバイル環境下ではアバターの非表示はガイドライン 1.a. に違反するように読める)
  • 2.a. Tweet text must be displayed on a line below the author’s name and @username, and may not be altered or modified in any way except as outlined in these requirements.
    • ツイートテキストがツイートした人の name と @username の上にかかれてしまっている。ただし、"outlined in these requirements" の例外事項に入っているのかも???
  • 3.a. Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
    • 返事を書く、リツイート、お気に入り のいずれのボタンも表示されない。
  • 6.b. The Twitter logo or Follow button for the Tweet author must always be displayed.
    • Twitter ロゴもフォローボタンも表示されていない。

cdn.api.twitter.com/1/statuses/show.json

<blockquote>〜</blockquote>の中に、ツイートにかんするすべての情報が入っているわけではない。 platform.twitter.com/widgets.js から以下のリクエストを出して、ツイートをリッチに表示するための情報を収集しているのである。

http://cdn.api.twitter.com/1/statuses/show.json?id=176199889313734657&include_entities=true&callback=twttr.tfw.callbacks.cb0&suppress_response_codes=true

id=で指定している数字はツイートIDであり、埋め込むツイートがかわるとここが変わる。 さらにおもしろいことにcallback=でJavaScriptのコールバック関数を指定しており(JSONP)、この引数をなくすと純粋なJSONとしてツイート情報が取得できる。

http://cdn.api.twitter.com/1/statuses/show.json?id=176199889313734657&include_entities=true&suppress_response_codes=true

ツイートが何のアプリケーションから投稿されたか、ツイート時刻(秒単位)といったツイートの詳細情報はもちろん、ツイートした人の情報(アカウント作成日時、使用言語、タイムゾーン)もレスポンスの中身に確認できる。 つまり、ツイートの表示されている以上の情報量があるのだ。

Twitter API v1.0 -> 1.1

実は Twitter API にも同じようなツイート情報取得用の API endpoint があるが、上述のとちがって簡単には使えない。

  • http://api.twitter.com/1/statuses/show.json?id=176199889313734657&include_entities=true
    • ただし、2013-03-05 以降はアクセス不能(Twitter API v1.0 廃止のため)
  • https://api.twitter.com/1.1/statuses/show.json?id=176199889313734657&include_entities=true
    • ただし、OAuth でのアクセストークンをもってアクセスしなければならない。

Twitter API 的には認証が必要な流れになっている(認証不要な Twitter API v1.0 は廃止される)のに対して、cdn.api.twitter.com は認証が不要である。 一方で、ツイートの埋め込みは認証がなくとも動かなければならないので、ツイート情報を認証なしで取得する手段はなくすことはできない。 仮に、ツイートの埋め込みのコードが全く異なる仕組みのもの(例えば IFRAME 使用とか)にしたとしても、現状のツイートの埋め込みのコードが動いたままにしなければならない。

以上の理由から、ツイート情報を認証なしで取得す るcdn.api.twitter.com による endpoint はなくならないと、筆者は考えている。 もっとも、文書化されていない隠し API に相当するので、細かい仕様の変更はあり得る。

ただ、Twitter API v1.1 で全 API endpoint に認証を必要とする理由に、高頻度で Twitter API にアクセスするボットのようなアプリケーションを把握することを挙げている(下記引用)。 cdn.api.twitter.com の隠し API endpoint はそういったボットのようなアプリケーションの抜け道に使用される可能性があり得ると思うのだが、そこのところはどうなるのだろうか楽しみだ。

For example, there are many applications that are pulling data from the Twitter API at very high rates (scraping, bots, etc.) where we only know the IP address of the applications. To prevent malicious use of the Twitter API and gain an understanding of what types of applications are accessing the API in order to evolve it to meet the needs of developers, it's important to have visibility into the activity on the Twitter API and the applications using the platform.

(2015-01-20加筆)ここで予期したとおり、この隠しAPIは廃止されたようだ。 現在はhttps://syndication.twitter.com/tweets.json?ids=176199889313734657&lang=ja&callback=twttr.tfw.callbacks.cb0&suppress_response_codes=true といった URL を発行している。 例のごとく https://syndication.twitter.com/tweets.json?ids=176199889313734657&lang=ja として JSON 形式で取得できるが、そもそも埋め込みツイートに最適化された内容になっており、 再利用性は低い。

参考文献