Person Data Extraction Methods

The helper methods defined for extracting Person data are:

  • opensocial.Person.getAppData

  • opensocial.Person.getDisplayName

  • opensocial.Person.getField

  • opensocial.Person.getId

  • opensocial.Person.isOwner

  • opensocial.Person.isViewer

The OpenSocial specification includes a number of alternate, streamlined methods for capturing a lot of the same social data you would through the aforementioned methods. These are embedded within the lightweight JavaScript API references.

Each method listed in the following sections supports several parameters, which are used to specify the data to be returned by the REST request to the container’s social endpoints.

osapi.people.get

get is the generic call within the lightweight JavaScript API methods to collect a user’s data, and can be used to retrieve most of the social details you will want to leverage.

The osapi.people.get(...) method accepts a JSON object to define the type of information you want, and takes the form:

osapi.people.get(params)

Parameter list

The params list may consist of:

auth (AuthToken)

An optional authorization token for requesting data.

Example value: HttpRequest.Authorization

userId (string or array of strings)

A string or array of strings that defines the user about whom you want to collect data. You may use short identifiers such as @me, @viewer, or @owner.

Example value: '@owner' or ['@me', '@owner']

groupId (string)

A string defining a group of people about whom you want to collect data. This value is especially useful when you’re trying to collect profile information for friends of the viewer or owner. You may use short identifiers such as @self or @friends.

Example value: '@friends'

fields (array of strings)

An array of strings identifying the Person fields you want to collect.

Example value: ['name', 'gender']

count (integer)

The number of results to be returned from the request.

Example value: 10 (return 10 results)

startIndex (integer)

The offset from the beginning at which results should be returned. You can use this parameter to skip starting results if they’re unwanted.

Example value: 5 (start at the fifth result)

startPage (integer)

An integer value that defines the first page you want results returned for. This option is useful when you’re trying to improve loading performance by reducing the processing time for Person data requests.

Example value: 2 (start at the second page of results)

Example request

Using the preceding parameters as a guide, we can put together a simple request to capture the application owner’s name and gender. We use the userId key with a value of @owner and then input name and gender under the fields key.

Finally, we bundle the request with the execute method to initiate the REST request to the container’s social endpoints.

//opensocial person data request
osapi.people.get({userId: '@owner', fields: ['name', 'gender']}).execute(
   function(result){
      var name = (result.name) ?
                 result.name.givenName + " " + result.name.familyName : "";
      var gender = (result.gender) ? result.gender : "Unknown";
   }
);

The execute method accepts a function reference to call once the REST request completes. The function will be called with the result set, after which we can parse the results to capture the data values we requested.

osapi.people.getViewer

The getViewer request is a convenience method to allow a developer to capture Person information for the person currently viewing the application. This method is the same as using the osapi.people.get(...) method with the following parameters set:

osapi.people.get({userId: '@viewer'})

Parameter list

The parameters available in the getViewer method are:

auth (AuthToken)

An optional authorization token for requesting data

Example value: HttpRequest.Authorization

fields (array of strings)

An array of strings identifying the Person fields you want to collect

Example value: ['name', 'gender']

Example request

As with the previous get method, we can use the getViewer method to capture social details. Since the request is targeted to the current application user, we don’t need to specify the userId parameter and only need the fields list:

//capture viewer data
osapi.people.getViewer({fields: ['name']}).execute(function(result){
  var name = (result.name) ? result.name.givenName + " " + result.name.familyName : "";
});

osapi.people.getViewerFriends

The getViewerFriends request is a convenience method to allow a developer to capture Person information for the friends or connections of the person currently viewing the application. This method is the same as using the osapi.people.get method with the following parameters set:

osapi.people.get({userId: '@viewer', groupId: '@friends'})

Parameter list

The parameters available in the getViewerFriends method are:

auth (AuthToken)

An optional authorization token for requesting data.

Example value: HttpRequest.Authorization

fields (array of strings)

An array of strings identifying the Person fields you want to collect.

Example value: ['name', 'gender']

count (integer)

The number of results to be returned from the request.

Example value: 10 (return 10 results)

startIndex (integer)

The offset from the beginning at which results should be returned. You can use this to skip starting results if they’re unwanted.

Example value: 5 (start at the fifth result)

startPage (integer)

An integer value that defines the first page you want results returned for. This option is useful when you’re attempting to improve loading performance by reducing the processing time for Person data requests.

Example value: 2 (start at the second page of results)

Example request

In this next example, we make a request to collect the friends of the current application viewer. Using the count parameter, we limit the number of friends returned to 10. These friends are returned as Person objects:

//get viewer friends
osapi.people.getViewerFriends({count: 10}).execute(function(result){
   var friends = result.list;
   var html = '';
   for (var i = 0; i < friends.length; i++){
      html += friends[i].name.givenName + ' : ' + friends[i].profileUrl + '<br />';
   }
});

Once the request returns, we store the friends array (result.list) in a variable. We then loop through the friends array, storing each friend’s name and profile URL.

osapi.people.getOwner

The getOwner request is a convenience method to allow a developer to capture PERSON information for the application owner, not the current viewer. This method is the same as using the osapi.people.get(...) method with the following parameters set:

osapi.people.get({userId: '@owner'})

Parameter list

The parameters available in the getOwner method are:

auth (AuthToken)

An optional authorization token for requesting data

Example value: HttpRequest.Authorization

fields (array of strings)

An array of strings identifying the Person fields you want to collect

Example value: ['name', 'gender']

Example request

Much like we did for the getViewer call, we will capture the name for the person (in this case, the application owner) but will extend it with the nickname. In many social networks, the nickname is the text identifier by which the person has chosen to be addressed. This nickname identifier is generally more publicly exposed than the full name:

//capture owner data
osapi.people.getOwner({fields: ['name', 'nickName']}).execute(function(result){
  var name = (result.name) ? result.name.givenName + " " 
           + result.name.familyName : "";
  name += (result.nickName) ? " : " + result.nickName : " : no nickname";
});

Once the request returns, we can capture the owner’s full name and nickname.

osapi.people.getOwnerFriends

The getOwnerFriends request is a convenience method to allow a developer to capture Person information for the friends of the application’s owner, not the current viewer’s friends. This method is the same as using the osapi.people.get(...) method with the following parameters set:

osapi.people.get({userId: '@owner', groupId: '@friends'})

Parameter list

The parameters available in the getOwnerFriends method are:

auth (AuthToken)

An optional authorization token for requesting data.

Example value: HttpRequest.Authorization

fields (array of strings)

An array of strings identifying the Person fields you want to collect.

Example value: ['name', 'gender']

count (integer)

The number of results to be returned from the request.

Example value: 10 (return 10 results)

startIndex (integer)

The offset from the beginning at which results should be returned. You can use this to skip starting results if they’re unwanted.

Example value: 5 (start at the fifth result)

startPage (integer)

An integer value that defines the first page you want results returned for. This option is useful when you’re attempting to improve loading performance by reducing the processing time for Person data requests.

Example value: 2 (start at the second page of results)

Example request

In this next example, we will collect the friends of the application’s owner. We will do so much like we did for the getViewerFriends call, but this time we will be capturing only the thumbnailUrl and only the second page of friend results, given a count of 10 friends. This means that we will be capturing friends 11–20:

//get owner friends
osapi.people.getOwnerFriends({'count': 10,
                              'startPage': 2,
                              'fields': ['thumbnailUrl']}).execute(function(result){
   var friends = result.list;
   var html = '';
   for (var i = 0; i < friends.length; i++){
      html += '<img src="' + friends[i].thumbnailUrl + '" alt="thumbnail image" />';
   }
});

Once the results have been returned, we loop through the array and store images for thumbnails of all the friends returned.

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

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