Saturday, March 13, 2010

OpenSocial 0.8 - Fetching Data








FETCHING VIEWER SOCIAL DATA WITH OPENSOCIAL 0.8

Profile information within a social network (e.g. MySpace, Orkut, YAP, Hi5) is something that users take painstaking amounts of time to input so that they can tell their friends and the world who they are. If a developer is building an application on one of these same platforms, that same profile detail is a gold mine of information that can be obtained to customize and personalize an application. Why would you ever ask a user to input their name, interests or gender if they already have this information freely available on their profile? Having the user re-enter this information is just asking for the user to drop off of your application. Using the knowledge wealth will both decrease user drop rates and make your applicatio more appealing to the masses.Within a container that supports the OpenSocial 0.8 JavaScript specification, fetching the profile information of a person can be accomplished and customized with several steps.

  1. "text/javascript">  
  2. //OpenSocial PERSON data request  
  3. var req = opensocial.newDataRequest();   
  4. var params = {};  
  5. params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [  
  6.     opensocial.Person.Field.NAME,  
  7.     opensocial.Person.Field.THUMBNAIL_URL  
  8. ];  
  9.   
  10. req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer_profile');  
  11. req.send(response);  
  12.   






The first thing we do is call newDataRequest to set up a new information request object within OpenSocial. Once this has been done we create our parameter list which will define what type of information we want to access within the information request. In our case here, we specify that we would like to capture “PeopleRequestFields”, namely a user’s “PROFILE_DETAILS”, and of those profile details we want to return the name and the thumbnail_url to their profile image. We can add in a whole series of profile information here, depending on what the container supports within the profile information specification. We then add to our request a newFetchPersonRequest and specify that we want the “VIEWER” profile (this could also be OWNER) and then input the params we set up to define what data we want from the viewer. Then we just name that request as “viewer_profile” so that we can access it later and send the request. The request function takes the name of the callback function as a parameter.


  1. "text/javascript">  
  2. //response handler  
  3. function response(data){  
  4.    var viewer = data.get('viewer_profile').getData();  
  5.    var userName =  
  6.       viewer.getField(opensocial.Person.Field.NAME);  
  7. }  
  8.  




When the request completes and gets to our callback function, our data is returned to us with the profile information of the viewer presuming that everything completed correctly. In the callback we can call get on the returned data, inputting the name of the request, and then call getData on that. If we want a finer granularity of control over the data that is returned, we can then call the getField function with the information that we want (in this case the viewer name).
That’s all there is to it – with a few calls you now have all the information you need to personalize your application to every users that uses it.

No comments:

Post a Comment