Acquiring an Avatar
Preliminary preparation
Each Mobage user has an avatar. An API is offered in Mobage Open Platform so that users can use their avatars within applications.
In order to use this API, you must define the mbga feature specifications with the Require element in the Gadget XML.
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Hello, world!">
<Require feature="opensocial-0.8" />
<Require feature="mbga" />
...
Acquiring a VIEWER’s avatar
The following is a sample for acquiring a VIEWER’s avatar.
var idSpec = opensocial.newIdSpec();
idSpec.setField(opensocial.IdSpec.Field.USER_ID, opensocial.IdSpec.PersonId.VIEWER);
idSpec.setField(opensocial.IdSpec.Field.GROUP_ID, opensocial.IdSpec.GroupId.SELF);
var opt_params = {};
mbga.requestFetchAvatar(idSpec, opt_params, function(response) {
if (response.hadError()) {
// error
} else {
var avatar = response.getData();
var url = avatar.getUrl();
// ...
}
});
- Create an IdSpec for the target user. Now specify VIEWER, OWNER, or an arbitrary user ID for USER_ID with an array. Only SELF can be specified for GROUP_ID.
- Make a request to acquire avatar information using the mbga.requestFetchAvatar() method.
- When the request is successful, the mbga.Avatar object is stored in DataResponse and is returned.
Specifying more detailed avatar attributes
Acquiring avatars from multiple users
var idSpec = opensocial.newIdSpec();
idSpec.setField(opensocial.IdSpec.Field.USER_ID, opensocial.IdSpec.PersonId.VIEWER);
idSpec.setField(opensocial.IdSpec.Field.GROUP_ID, opensocial.IdSpec.GroupId.SELF);
var avatar_params = {};
avatar_params[mbga.Avatar.Field.SIZE] = mbga.Enum.Avatar.Size.X_LARGE;
avatar_params[mbga.Avatar.Field.EMOTION] = mbga.Enum.Avatar.Emotion.SMILE;
avatar_params[mbga.Avatar.Field.TYPE] = mbga.Enum.Avatar.Type.IMAGE;
avatar_params[mbga.Avatar.Field.EXTENSION] = mbga.Enum.Avatar.Extension.GIF;
var opt_params = {};
opt_params[mbga.AvatarRequestFields.AVATAR] = avatar_params;
mbga.requestFetchAvatar(idSpec, opt_params, function(response) {
if (response.hadError()) {
// error
} else {
var avatar = response.getData();
var url = avatar.getUrl();
}
});
Acquiring avatars from multiple users
Avatars from multiple users can be obtained simultaneously. Follow the steps below to acquire avatars from the VIEWER’s Mobatomo (Friends).
var params = {};
params[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
params[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.FRIENDS;
var idSpec = opensocial.newIdSpec(params);
var req = opensocial.newDataRequest();
req.add(req.newFetchPeopleRequest(idSpec), "friends");
req.send(function(response) {
if (response.hadError()) {
// error
} else {
var friendIds = [];
var friends = response.get("friends").getData();
friends.each(function(friend) {
friendIds.push(friend.getId());
});
idSpec = opensocial.newIdSpec();
idSpec.setField(opensocial.IdSpec.Field.USER_ID, friendIds);
idSpec.setField(opensocial.IdSpec.Field.GROUP_ID, opensocial.IdSpec.GroupId.SELF);
var opt_params = {};
mbga.requestFetchAvatar(idSpec, opt_params, function(response) {
if (response.hadError()) {
// error
} else {
var avatars = response.getData();
avatars.each(function(avatar) {
var url = avatar.getUrl();
// ...
});
}
});
}
});
- First, acquire a VIEWER’s Mobatomo (Friends) with the newFetchPeopleRequest() method.
- Create an array of Mobatomo (Friends) user IDs.
- Make this array an argument for the requestFetchAvatar() method, and make a request to acquire information on the avatars.
- Process the DataResponse when multiple users are specified, as the collection of Avatar objects will be stored in it.
Revision History
- 12/2010
- Initial Release.