Friday, November 2nd, 2007

OpenSocial: Social JavaScript APIs

Category: Google, JavaScript

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:

  1. <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:

You will probably want to grab some social data which requires you to setup a request and grab data:

javascript

  1. var req = opensocial.newDataRequest();
  2. req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  3. req.add(req.newFetchPersonRequest('OWNER'), 'owner');
  4. req.add(req.newFetchPeopleRequest('OWNER_FRIENDS'), 'ownerFriends');
  5. req.send(function(data) {
  6.   viewer = dataResponse.get("viewer").getData();
  7.   owner = dataResponse.get("owner").getData();
  8.   ownerFriends = dataResponse.get("ownerFriends").getData().asArray() || [];
  9. });

When you want to work with user data such as preferences, you will use the storage api. Here is an example from WoShamBo:

javascript

  1. function saveHistory() {
  2.   var write_request;
  3.   var req = opensocial.newDataRequest();
  4.   debugLog("Writing " + JSON.stringify(my_history) + "<br />");
  5.   debugLog("to " + current_opponent.getId() + "<br />");
  6.   req.add(req.newUpdatePersonAppDataRequest('VIEWER', current_opponent.getId(), JSON.stringify(my_history)));
  7.   req.send(saveHistoryCallback);
  8. }
  9.  
  10. function saveHistoryCallback(dataResponse) {
  11.   if (dataResponse.hadError()) {
  12.     debugLog("Error occurred while saving.<br />");
  13.   } else {
  14.     debugLog("The history was saved.<br />");
  15.   }
  16.   refreshDisplays();
  17. }

Resources

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.

Posted by Dion Almaer at 3:35 am
3 Comments

+++--
3.2 rating from 21 votes

3 Comments »

Comments feed TrackBack URI

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.

Comment by mhr — November 2, 2007

Is it only me or is youtube so slow it’s starting to become useless especially for large videos…?

Comment by Thomas Hansen — November 4, 2007

Useless indeed!
you tube is never useless

Comment by Aphrodisiac — July 31, 2008

Leave a comment

You must be logged in to post a comment.