Gadgets <?xml version="1.0" encoding="UTF-8" ?><Module> <ModulePrefs title="Hello Social!"> <Require feature="opensocial-0.8" /> </ModulePrefs> <Content type="html"> <![CDATA[ ... ]]> </Content> </Module> The OpenSocial JavaScript API is a gadget feature, too! Live Demo
The OpenSocial JavaScript API
Making Gadgets Social
Gadgets can become social with OpenSocial JavaScript API
OpenSocial API has three core services:
People & Friends
Access profile information
Access friends information
Activities
See what you’re friends are up to
Share what you are doing
Persistence
Provide state without a server
Share data with your friends
The OpenSocial JavaScript API
Representing users:
Client-side, users must work with the VIEWER and the OWNER .
The OpenSocial JavaScript API
Multiple personalities:
When you visit your own profile, you are both the VIEWER and the OWNER .
The OpenSocial JavaScript API
OpenSocial requests:
An OpenSocial DataRequest is created.
Requests are added to the DataRequest.
The DataRequest is sent to the server asynchronously.
When the request finishes, the supplied callback will be called.
function request() { var req = opensocial.newDataRequest(); req.add(req.newFetchPersonRequest("OWNER"), "get_owner"); req.add(req.newFetchPersonRequest("VIEWER"), "get_viewer"); req.add(req.newFetchActivitiesRequest("VIEWER"), "vactivities"); req.add(req.newFetchPersonAppDataRequest("OWNER", "*"), "odata"); ... req.send ( response );}; function response (data) { ... }; gadgets.util.registerOnLoadHandler(request);
The OpenSocial JavaScript API
OpenSocial responses:
Responses are bundled according to the keys specified in the request.
Check for an error at the global response level .
Check for an error at the specific response level .
Use getData() to retrieve the actual information in a request.
function response(data) { if ( data.hadError() ) { if ( data.get("get_owner").hadError() ) { ... } if ( data.get("get_viewer").hadError() ) { ... } ... } var owner = data.get ( "get_owner"). getData (); var viewer = data.get( "get_viewer"). getData (); };
The OpenSocial JavaScript API Working with people:
opensocial.Person - JavaScript representation of a user.
The OpenSocial JavaScript API Request one person: req.add(req.newFetchPersonRequest( idspec , opt_params ), "key");
idspec can be either “VIEWER”, “OWNER” or an ID number.
opt_params contains extra request parameters, such as which profile fields to fetch.
newFetchPersonRequest responses: var owner = data.get("key").getData(); alert(owner.getDisplayName());
Data contains a single opensocial.Person object.
Person objects can contain lots of information, such as addresses, companies, phone numbers, favorite movies, and thumbnail urls.
The OpenSocial JavaScript API Methods available on an OpenSocial Person:
getDisplayName() Gets a text display name for this person; guaranteed to return a useful string.
getField(key, opt_params) Gets data for this person that is associated with the specified key.
getId() Gets an ID is permanently associated with this person.
isOwner() Returns true if this person object represents the owner of the current page.
isViewer() Returns true if this person object represents the currently logged in user.
Live Demo
The OpenSocial JavaScript API
ABOUT_ME
ACTIVITIES
ADDRESSES
AGE
BODY_TYPE
BOOKS
CARS
CHILDREN
CURRENT_LOCATION
DATE_OF_BIRTH
DRINKER
EMAILS
ETHNICITY
FASHION
FOOD
GENDER
HAPPIEST_WHEN
HAS_APP
HEROES
HUMOR
ID
INTERESTS
JOB_INTERESTS
JOBS
LANGUAGES_SPOKEN
LIVING_ARRANGEMENT
LOOKING_FOR
MOVIES
MUSIC
NAME
NETWORK_PRESENCE
NICKNAME
PETS
PHONE_NUMBERS
POLITICAL_VIEWS
PROFILE_SONG
PROFILE_URL
PROFILE_VIDEO
QUOTES
RELATIONSHIP_STATUS
RELIGION
ROMANCE
SCARED_OF
SCHOOLS
An OpenSocial Person's fields:
SEXUAL_ORIENTATION
SMOKER
SPORTSSTATUSTAGS
THUMBNAIL_URL
TIME_ZONE
TURN_OFFS
TURN_ONS
TV_SHOWS
URLS
The OpenSocial JavaScript API Working with people:
A Collection represents many opensocial.Person objects.
The OpenSocial JavaScript API Request many people: var idspec = opensocial.newIdSpec({ “ userId” : “OWNER”, “groupId” : “FRIENDS”}); req.add(req.newFetchPeopleRequest( idspec , opt_params ), "key");
idspec is an object that can represent groups of people. “userId” can be “VIEWER” or “OWNER” or an ID, and “groupId” can be “SELF”, “FRIENDS”, or the name of a group.
opt_params contains extra request parameters, such as which profile fields to fetch, and how to order or filter the returned people.
Process the response: var owner_friends = data.get("key").getData(); owner_friends.each(function (person) { alert(person.getDisplayName()); });
Data contains a Collection of opensocial.Person objects.
Live Demo
The OpenSocial JavaScript API Working with data:
Persistent data gives apps key, value storage directly on the container.
String only, but conversion to JSON allows for storage of complex objects.
Storage per app per user - scales well with growth.
Ideal for settings, customizations.
The OpenSocial JavaScript API Set persistent data: req.add(req.newUpdatePersonAppDataRequest( idspec , key , value ));
idspec can only be “VIEWER”.
key is the name under which this data will be stored.
value is a string representing the data to store.
The OpenSocial JavaScript API Fetch persistent data: var idspec = opensocial.newIdSpec({ "userId" : "OWNER", "groupId" : "SELF" }); req.add(req.newFetchPersonAppDataRequest( idspec , keys ), "key"); req.add(req.newFetchPersonRequest("OWNER"), "ownerkey");
idspec is an object that can represent groups of people, the same as newFetchPeopleRequest.
keys is a list of persistent data keys to retrieve the data for.
The owner is requested because the data returned is indexed by user ID and we want the owner’s data.
newFetchPersonAppDataRequest responses: var app_data = data.get("key").getData(); var value = app_data[owner.getId()][key];
The OpenSocial JavaScript API Working with activities:
API to post information about what users are doing with your app.
Many containers have support for images and some HTML.
Channel to grow your application.
orkut MySpace hi5
The OpenSocial JavaScript API Post an activity: function postActivity( text ) { var params = {}; params[opensocial.Activity.Field.TITLE] = text ; var activity = opensocial.newActivity (params); opensocial.requestCreateActivity (activity, opensocial.CreateActivityPriority.HIGH, callback); };
Assign the activity text to the TITLE field.
Call opensocial.newActivity() to create a new Activity instance.
Call opensocial.requestCreateActivity() to post the activity to the container.
RESTful and RPC protocols Authentication: Both protocols use OAuth to identify users and apps. Depending on what the application needs to do, it can use two-legged or three-legged OAuth.
Two-legged OAuth:
The application authenticates directly with the container .
Perform non-user specific operations:
Update persistent data for app users.
Can request information for users who have shared their profile information with the app.
Three-legged OAuth:
The user tells the container to give profile access to the application .
Perform user specific operations:
Post activities.
Fetch friends of the current user.
RESTful and RPC protocols
Client libraries are being created for PHP, Java, Python, Ruby.
Help you connect to OpenSocial containers, and work with social data on your server.
Sample: Login, get thumbnail and friends. OpenSocialClient c = new OpenSocialClient("myspace.com"); c.setProperty(OpenSocialClient.Properties.REST_BASE_URI, "http://api.myspace.com/v2/"); c.setProperty(OpenSocialClient.Properties.CONSUMER_SECRET, <MYSPACE_SECRET>); c.setProperty(OpenSocialClient.Properties.CONSUMER_KEY, <MYSPACE_APP_KEY>); c.setProperty(OpenSocialClient.Properties.VIEWER_ID, <YOUR_MYSPACE_ID>); OpenSocialPerson p = c.fetchPerson(<YOUR_MYSPACE_ID>); OpenSocialField f = p.getField("thumbnailUrl"); System.out.println(f.getStringValue()); Collection<OpenSocialPerson> friends = c.fetchFriends(<YOUR_MYSPACE_ID>); for (OpenSocialPerson friend : friends) { System.out.println(friend.getDisplayName()); }
0 comments
Post a comment