mixiアプリでマイミク情報を取得するサンプルコードまとめ(1)

iPhoneからもdeveloper登録ができるようにったmixiアプリ
@ITの記事で丁寧にmixi アプリ(Opensocialアプリ)の作り方を説明しているエントリーが盛り上がっていたので便乗してみます。

スパイスラボ神部(id:ryuzi_kambe)さんが先日書いた記事のブクマで「サンプルコード付きのもっとスマートなまとめがあってもいい気がしますが…」と書かれてたのでまとめてみました。

mixi アプリで何が取得出来るのか?のまとめ ( ラボブログ )
http://blog.spicebox.jp/labs/2009/06/mixi_23.html?utm_source=labblogrss&utm_medium=rss

javascriptは普段書かないので(言い訳)おかしなところあったらご指摘御願いします。

基本的な概念のおさらい

アプリでユーザーデータを扱うときは「VIEWER」と「OWNER」を意識する

mixiアプリは各ユーザーにインストール(ローカルにインストールじゃないですよ)されて初めて使用できるようになります。
アプリをインストールしたユーザー(アプリ保持者)のことをOWNERと呼び、例えば友達のアプリを見ている(利用している)人はVIEWERと区別されます。

プロフィール情報

下記の情報を取得できます。
ただし、公開範囲に「全体公開」が設定されているものしか取得できません!

  • ID
  • ニックネーム
  • プロフィール写真
  • プロフィールURL
  • 現住所(県のみ)
  • 年齢
  • 生年月日
  • 性別
  • 血液型

詳しくは下記のURLをご参照あれ。
http://developer.mixi.co.jp/appli/pc/lets_enjoy_making_mixiapp/get_mymixi_info

サンプルコード(公式のものを拝借しつつちょっと変更)
<?xml version="1.0" encoding="UTF-8"?>

<Module>
  <ModulePrefs title="getViewerProfile">
    <Require feature="opensocial-0.8" />
  </ModulePrefs>
  <Content type="html"><![CDATA[
<div>Hello, <span id="target"></span>!</div>
<script type="text/javascript">
function init() {
    var p = {};
    p[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [
        opensocial.Person.Field.PROFILE_URL,
        opensocial.Person.Field.ADDRESSES,
        opensocial.Person.Field.GENDER,
        mixi.PersonField.BloodType
    ];
    var req = opensocial.newDataRequest();
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER, p), "viewer");
    req.send(function(data) {
        // [注意]全体公開にしている情報しか取得できない!!
        var viewer = data.get("viewer").getData();
        var id = viewer.getId();
        var displayName = viewer.getDisplayName();
        var thumbnailUrl = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
        var profileUrl = viewer.getField(opensocial.Person.Field.PROFILE_URL);
        var address = viewer.getField(opensocial.Person.Field.ADDRESSES)[0].getField(opensocial.Address.Field.UNSTRUCTURED);
        var name = viewer.getField(opensocial.Person.Field.NAME).getField(opensocial.Name.Field.UNSTRUCTURED);
        var nickName = viewer.getField(opensocial.Person.Field.NICKNAME);
        var gender = viewer.getField(opensocial.Person.Field.GENDER).getDisplayValue();
        var boodType = viewer.getField(mixi.PersonField.BloodType);
        document.getElementById("target").innerHTML = displayName;
    });
}
gadgets.util.registerOnLoadHandler(init);
</script>

  ]]></Content>
</Module>

マイミク情報の取得

マイミクの情報が取得できます。

サンプルコード(id:amachangさんのコードを参考に)
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="getFriendList">
    <Require feature="opensocial-0.8"/>
  </ModulePrefs>
  <Content type="html">
  <![CDATA[
<script type="text/javacript">
    var req = opensocial.newDataRequest();
    // OWNERのマイミクを取得
    req.add(req.newFetchPeopleRequest(opensocial.newIdSpec({ userId: 'OWNER', groupId: 'FRIENDS' }), { max: 1000 }), 'friends');
    // VIEWERのマイミクを取得取得したい場合、
    // req.add(req.newFetchPeopleRequest(opensocial.newIdSpec({ userId: 'VIEWER', groupId: 'FRIENDS' }), { max: 1000 }), 'friends');
    req.send(function(data) {
        var friendsObject = data.get('friends').getData();
        // asArray() ? -> http://code.google.com/intl/ja/apis/opensocial/docs/0.8/reference/#opensocial.Collection.asArray
        var friendsArray = friendsObject.asArray();
        var friends = friendsArray.map(function(friend) {
            return friend.getDisplayName();
        }); 
        document.body.innerHTML = friends;
        // 上の6行を1行で書くと
        // document.body.innerHTML = data.get('friends').getData().asArray().map(function(friend){ return friend.getDisplayName()});
    });
</script>
  ]]>
  </Content>
</Module>

OpenSocialアプリ盛り上がるといいな。

もっと知りたい人はここ読むといいと思います。

OpenSocial API リファレンス(OpenSocial API v0.8.1)
http://code.google.com/intl/ja/apis/opensocial/docs/0.8/reference/

gooホームガジェット - goo Developer's Kitchen
http://developer.home.goo.ne.jp/document/goo%E3%83%9B%E3%83%BC%E3%83%A0%E3%82%AC%E3%82%B8%E3%82%A7%E3%83%83%E3%83%88