Capturing the User Profile

Due to the vast array of methods, social access functions, and container social hooks available, a developer has numerous options for accessing a user’s profile data. Two of the most popular consist of standardized AJAX request methods; both provide the same results, but they have radically different ways of getting there.

Old method

The traditional AJAX social request method—used as the primary request functionality up until OpenSocial 0.9—is newDataRequest. Even though it is more laborious, this method is widely used because it is backward compatible.

While method names and parameters for different data fetches change, you still have to call standard methods to set up a data request. When making a get request, we first set up our data request object using the newDataRequest method, as follows:

var req = opensocial.newDataRequest();

Now that the data request container object is available, we can create the key value parameter list to define the profile data that we capture. Our key indicates that we want to capture PROFILE_DETAILS, and the value array contains the list of OpenSocial Person fields that we want to capture. In this example, we are capturing the user’s name and thumbnail URL:

var params = {};
params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [
   opensocial.Person.Field.NAME,
   opensocial.Person.Field.THUMBNAIL_URL
];

We then add a newFetchPersonRequest call to our initial data request to indicate that we want to capture Person data from a profile. We will pass in two parameters to this function: VIEWER and params. VIEWER defines the person about whom we would like to capture data. This will normally be either VIEWER (the person using the application) or OWNER (the person who created the application). The params list is the object we created earlier in the script that lists the fields we would like to capture. The second parameter of the add function is the label for the request:

req.add(req.newFetchPersonRequest('VIEWER', params),
   'viewer_profile'),

We then send that request to our response handler (personCallback). This method will issue an AJAX request to capture the data required:

req.send(personCallback);

Much like with a standard AJAX request, the callback function will take in a parameter (data) that is the object sent back from the container servers. You could parse this object to find the data that you are looking for, but that would require unnecessary effort; there are helper methods set up within OpenSocial to parse the returned data structure, after all, and we should take advantage of them:

//response handler
function personCallback(data){
   var viewer = data.get('viewer_profile').getData();
   var name = viewer.getDisplayName();
   var thumb = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
}

First, we can retrieve the data structure that we are looking for by using the data.get(...) method with the syntax of data.get('label').getData(), where label is the text that we assigned to the data request in the previous sample. This will return the response data object to us. There are a few commonly used data sets that have their own methods—such as getDisplayName() to return the name of the user (which we have used in the preceding sample) or getId() to capture the user ID—but for the most part, we’ll use the getField() method to capture specific Person details. From our callback, we pull out these details by calling getField on our response data object, followed by specifying the field that we want to capture as the parameter. With all of that, you can now capture any user profile details.

New method

With the introduction of the lightweight JavaScript APIs in OpenSocial 0.9 came new and easier methods to streamline the development process and reduce code bloat.

We can accomplish our data retrieval task quickly and easily using OpenSocial’s getViewer or getOwner methods:

//opensocial person data request
osapi.people.getViewer({fields: ['name']}).execute(function(data){
  alert('Hello ' + data.name);
});

To request details about a person, you need to take a few steps (all chained nicely together, though). Breaking down the request into its individual parts, we have three distinct sections:

  • osapi.people.getViewer(...) is the method specifying that we want to capture the viewer’s profile information. The parameter that we pass into this method is a JSON structure, which defines the fields that we want to return from the profile. In this case, we want to capture the viewer’s name.

  • execute(...) will initialize the viewer data request.

  • The callback, listed as the parameter in the execute function, will be hit when the viewer data request completes, and will contain the Person object returned (result parameter). From this result, we can use standard dot notation to get at the data we requested.

Comparing the old and new methods, we can see that the lightweight JavaScript APIs produce a smaller code footprint with additional layers of method chaining.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.55.198