中国版twitter「新浪微博」のAPIの使い方

最近(やっと)使い始めた中国版twitter「新浪微博」、フォロワーにすごい勢いで中国語を添削されたりしながらも、前の同僚を発見したりして楽しく使っている。しかしこの新浪微博、本当に良くできている。「単なるTwitterのパクリ」と思っている人はぜひとも使ってみてほしい。言語の壁さえ突破することができれば、近いうちに中国発の Web サービスが世界でヒットする日も近いかもしれない。何といっても、膨大な国内市場という後ろ盾があるからね。

さて、こうすると「APIとか使ってデータを取得してアプリ開発や自然言語処理やテキストマイニングができたら面白いなぁ」と思うのが研究者魂。ということで早速使ってみた。

英語版が無いのが残念だが、API のドキュメントはここにある。基本的に何でもできるようだ。

API文档 – 新浪微博API

API を使うには、まず開発者登録、続いて、アプリケーション登録をする必要がある。もちろん、新浪微博のユーザー登録自体は済んでいるものと仮定する。まず、開発者登録は

我的应用

から入り、利用者の別(個人・企業)、開発者名、メールアドレス、IM のアカウント、電話番号、Webページを登録しよう。開発者の登録の次に、アプリケーションを登録する必要がある。次に、「创建应用」のボタンを押して、各種必要事項を入力する。必要事項には、应用名称(=アプリケーション名)、应用地址(=アドレス)、应用介绍(=紹介)、域名绑定(=ドメイン制限)、应用分类(=タイプ)、标签(=タグ)がある。とりあえず API をテストで使うだけなので適当に入力した。

登録が終わると、「应用状态(=アプリケーションの状態)」ページに遷移する。ここで、App Key と App Secret が書かれている。これはアプリケーションの認証に使うのできちんと確認できるようにしておく。

ここまで準備ができれば、まずは public timeline を取得してみる。当該 API の解説はこのページにある:

Statuses/public timeline – 新浪微博API

結果を XML で取得するには、以下 URL を GET する。例えば、wget を使うなら、

wget “http://api.t.sina.com.cn/statuses/public_timeline.xml?source={appkey}”

とすればよい。{appkey}には、上の「应用状态」の画面で確認した App Key を入れる。public timeline を取得するだけならユーザー認証は必要ない。以下のような XML が返される。

<?xml version="1.0" encoding="UTF-8"?><statuses>
  <status>
    <created_at>Sat Jan 22 11:54:44 +0800 2011</created_at>
    <id>5483835765</id>
    <text>一早起来没有了昨晚的酒气,洗碗擦马桶送朋友出门。
我想起房产证还没拿,维修的音响还没拿,身份证没换。oh,有得忙了。
给力加油!</text>
    <source>
      <a href="http://t.sina.com.cn/mobile/iphone.php">iPhone
客户端</a>
    </source>
    <favorited>false</favorited>
    <truncated>false</truncated>
    <geo/>
    <in_reply_to_status_id></in_reply_to_status_id>
    <in_reply_to_user_id></in_reply_to_user_id>
    <in_reply_to_screen_name></in_reply_to_screen_name>
    <user>
      <id>1496993882</id>
      <screen_name>Teeniee</screen_name>
      <name>Teeniee</name>
      <province>44</province>
      <city>1000</city>
      <location>广东</location>
      <description>杂志人。独立摄影师。数码生活提倡者。</description>
      <url>http://teeniee.blog.163.com</url>
      <profile_image_url>http://tp3.sinaimg.cn/1496993882/50/1290704563/1
</profile_image_url>
      <domain>teen1626</domain>
      <gender>m</gender>
      <followers_count>185</followers_count>
      <friends_count>144</friends_count>
      <statuses_count>399</statuses_count>
      <favourites_count>14</favourites_count>
      <created_at>Thu Apr 08 00:00:00 +0800 2010</created_at>
      <following>false</following>
      <verified>false</verified>
      <allow_all_act_msg>false</allow_all_act_msg>
      <geo_enabled>true</geo_enabled>
    </user>
    <annotations/>
  </status>
  <status>
    <created_at>Sat Jan 22 11:54:46 +0800 2011</created_at>
    <id>5483835767</id>
    <text>回家了,回家了,</text>
    <source>
      <a href="http://t.sina.com.cn">新浪微博</a>
    </source>
    <favorited>false</favorited>
    <truncated>false</truncated>
...

次に、user timeline を使ってみる。当該 API の解説は以下のURLにある:

Statuses/user timeline – 新浪微博API

とりあえず簡単な Basic 認証による方式を使う。wget ならば、

wget –http-user={user} –http-passwd={pass} “http://api.t.sina.com.cn/statuses/user_timeline.xml?source={appkey}&user_id=11051″

となる。{user}と{pass}は、新浪微博にログインするためのあなたのパスワードだ。{appkey}は上記と同じ、”user_id=” のところには暫定的に「微博开放平台1」のユーザーID が入っているが、任意のユーザーのIDを入れる。これで、以下のような XML が返される:

<?xml version="1.0" encoding="UTF-8"?><statuses>
  <status>
    <created_at>Mon Dec 27 17:48:33 +0800 2010</created_at>
    <id>4760037013</id>
    <text>各位亲爱的开发者,感谢大家长期以来对微博开放平台的支持![呵呵]
目前,开放平台官方技术支持账号已经启动了,大家有技术相关的问题可以直接
@微博API或者给这个账号发私信,每天都会有技术人员在线为大家解答问题的~
关于审核标准、平台政策相关的问题可以@微博开放平台 再次感谢大家的支持~
</text>
    <source>
      <a href="http://t.sina.com.cn">新浪微博</a>
    </source>
    <favorited>false</favorited>
    <truncated>false</truncated>
    <geo/>
    <in_reply_to_status_id></in_reply_to_status_id>
    <in_reply_to_user_id></in_reply_to_user_id>
    <in_reply_to_screen_name></in_reply_to_screen_name>
    <user>
      <id>11051</id>
      <screen_name>微博开放平台1</screen_name>
      <name>微博开放平台1</name>
      <province>11</province>
      <city>8</city>
      <location>北京 海淀区</location>
      <description>新浪微博开放平台市场推广官方账号,如有技术问题,
请@微博API或者发私信给微博API</description>
      <url>http://open.t.sina.com.cn/</url>
      <profile_image_url>http://tp4.sinaimg.cn/11051/50/1280283165/1
</profile_image_url>
      <domain></domain>
      <gender>m</gender>
      <followers_count>262</followers_count>
      <friends_count>0</friends_count>
      <statuses_count>0</statuses_count>
      <favourites_count>0</favourites_count>
      <created_at>Wed Jan 20 00:00:00 +0800 2010</created_at>
      <following>false</following>
      <verified>false</verified>
      <allow_all_act_msg>true</allow_all_act_msg>
      <geo_enabled>true</geo_enabled>
    </user>
    <annotations/>
  </status>
  <status>
    <created_at>Wed Dec 22 17:32:29 +0800 2010</created_at>
    <id>4640348615</id>
    <text>PCworld:Facebook CEO马克·扎克伯格今天上午造访新浪总部,
并与新浪高管进行了会谈。扎克伯格谈了他对中国互联网市场的看法,并希望了解
新浪微博这一目前中国最热门的互联网产品http://sinaurl.cn/hbd1Bb</text>
    <source>
      <a href="http://t.sina.com.cn">新浪微博</a>
    </source>
    <favorited>false</favorited>

...

なお、各言語の SDK が

SDK – 新浪微博API

から手に入るので参照してみると良い。

This entry was posted in language. Bookmark the permalink.
add to hatena hatena.comment (17) add to del.icio.us (0) add to livedoor.clip (1) add to Yahoo!Bookmark (0) Total: 18

One Response to 中国版twitter「新浪微博」のAPIの使い方

  1. Pingback: Twitter Trackbacks for 中国版twitter「新浪微博」のAPIの使い方 - UNLP Blog [lilyx.net] on Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>