Friday, November 2nd, 2007
OpenSocial: Social JavaScript APIs

This is a site about technology for developers, so I won't get into any politics on OpenSocial and Facebook. There is already more coverage on that than you could want to read.
How is a social API interesting to Ajax developers? The main interface to the social data is through JavaScript. You can also use REST to hit the endpoints themselves, but the JavaScript API is very much the first class citizen.
If you are used to the Google Gadgets API you will probably be most familiar. Social components just have to include a one liner to their ModulePrefs:
-
-
<require feature="opensocial-0.5"/>
-
This tells the container that you will be doing social tasks. There are three main APIs that you now have access too:
- People Data API: You can get access to owners and viewers, and their friends
- Persistence Data API: Store simple hash table data for the users using your application
- Activities Data API: Like the Facebook News feed
You will probably want to grab some social data which requires you to setup a request and grab data:
-
-
var req = opensocial.newDataRequest();
-
req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
-
req.add(req.newFetchPersonRequest('OWNER'), 'owner');
-
req.add(req.newFetchPeopleRequest('OWNER_FRIENDS'), 'ownerFriends');
-
req.send(function(data) {
-
viewer = dataResponse.get("viewer").getData();
-
owner = dataResponse.get("owner").getData();
-
ownerFriends = dataResponse.get("ownerFriends").getData().asArray() || [];
-
});
-
When you want to work with user data such as preferences, you will use the storage api. Here is an example from WoShamBo:
-
-
function saveHistory() {
-
var write_request;
-
var req = opensocial.newDataRequest();
-
debugLog("Writing " + JSON.stringify(my_history) + "<br />");
-
debugLog("to " + current_opponent.getId() + "<br />");
-
req.add(req.newUpdatePersonAppDataRequest('VIEWER', current_opponent.getId(), JSON.stringify(my_history)));
-
req.send(saveHistoryCallback);
-
}
-
-
function saveHistoryCallback(dataResponse) {
-
if (dataResponse.hadError()) {
-
debugLog("Error occurred while saving.<br />");
-
} else {
-
debugLog("The history was saved.<br />");
-
}
-
refreshDisplays();
-
}
-
Resources
- Articles: How To Build Your First Social Gadget, Gadget tutorial, Social design best practices, and How To Read and Write Shared Social Data
- OpenSocial JavaScript Developer Guide
- Example applications
- OpenSocial FAQ
Google Campfire One
NOTE: I have posted some personal thoughts on OpenSocial and how people are focusing on the wrong points on my own blog. Also, Google Code relaunched with a nice new jQuery powered interface.












so, the next big thing is singing country songs in the woods about open API’s, mankind sharing their family photos online and everyone getting happy.
happy developers happy shareholders.
Is it only me or is youtube so slow it’s starting to become useless especially for large videos…?
Useless indeed!
you tube is never useless