Feb 25, 2008
OpenSocial JavaScript API にチャレンジ(2)
Friends データへのアクセス
今回もチュートリアルに沿って遊んでみる。 友人データを API 経由で取得して一覧表示する。 ちなみに、チュートリアルのコードは Orkut sandbox (OpenSocial API ver.0.7) では動作しなかった。
- OpenSocial Tutorial
- http://code.google.com/apis/opensocial/articles/tutorial/tutorial-0.7.html
- API Reference - opensocial
- http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.html
- API Refenrece - opensocial.DataRequest
- http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.DataRequest.html
- API Refenrece - opensocial.DataRequest.PeopleRequestFields
- http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.DataRequest.PeopleRequestFields.html
- API Reference - opensocial.Person
- http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.Person.html
listfriends.xml
Orkut へのディプロイ方法は前回と同じ。
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="List Friends">
<Require feature="opensocial-0.7"/>
<Require feature="settitle"/>
<Require feature="views"/>
</ModulePrefs>
<Content type="html">
<![CDATA[
<script>
// <!--
function getElement (id) {
if (document.getElementById){
return document.getElementById(id);
} else if (document.all) {
return document.all[id];
} else if (document.layers) {
return document.layers[id];
} else {
return null;
}
}
function loadData() {
// prepare request
var request = opensocial.newDataRequest();
// prepare required details
var required = new Array();
required[0] = opensocial.Person.Field.ID;
required[1] = opensocial.Person.Field.NAME;
required[2] = opensocial.Person.Field.THUMBNAIL_URL;
required[3] = opensocial.Person.Field.PROFILE_URL;
// prepare parameters
var parameters = {};
parameters[opensocial.DataRequest.PeopleRequestFields.MAX] = 1000;
parameters[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = required;
// send request
request.add(request.newFetchPeopleRequest('OWNER_FRIENDS', parameters), 'friends');
request.send(onLoadData);
}
function onLoadData(data) {
// get all friends data
var friends = data.get('friends').getData().asArray();
// create html snippet
var s = "<ul>";
for (var i=0; i<friends.length; i++) {
var person = friends[i];
var id = person.getId();
var name = person.getField(opensocial.Person.Field.NAME);
var firstname = name.getField(opensocial.Name.Field.GIVEN_NAME);
var lastname = name.getField(opensocial.Name.Field.FAMILY_NAME);
var displayname = person.getDisplayName();
var picUrl = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
var profileUrl = person.getField(opensocial.Person.Field.PROFILE_URL);
s += "<li>";
s += "<a href='" + profileUrl + "'>" + displayname + "</a>";
s += "</li>";
}
s += "</ul>";
// display all friends
getElement("friends").innerHTML = s;
}
// registerOnLoadHandler だけだとスタートしない Web ブラウザがある。何故??
gadgets.util.registerOnLoadHandler(loadData);
loadData();
// -->
</script>
<body>
<div id="friends"></div>
</body>
]]>
</Content>
</Module>
実行結果
TrackBack ping me at
http://www.in-vitro.jp/blog/index.cgi/SNS/20080225_01.trackback
writeback message: Ready to post a comment.

![[remote access]](/blog/entries/SNS/20080225_01/orkut_listfriends.png)