Live OpenSocial editor

You can type in the field below and press the button to execute OpenSocial code. This is a good place to copy and paste code examples for experimenting with the OpenSocial API.

In addition to the standard JavaScript and OpenSocial methods, you may also call the output method to print output text. output can take a variable number of arguments, so you can print several variables on the same line without needing to construct the string yourself. If you have firebug installed and enabled, output will also echo output to the firebug console.

Requesting profile data

This example displays profile information for the various classes of people exposed by the OpenSocial API.

The request function requests profile data for VIEWER, OWNER, VIEWER_FRIENDS, and OWNER_FRIENDS. request specifies response as the callback function for this request.

The response function defines an output method called printPerson which prints the thumbnail and profile URLs for the supplied Person object. response then iterates through each of the requested groups and calls printPerson on each record returned.

Working with Person AppData

The following example will demonstrate setting app data for the viewer (you).

request generates nondeterministic values for two data keys - sample_time (the current time) and sample_rand (a random number from 0 to 99 inclusive). Then it sends a request to store these two values in the VIEWER's personAppData and specifies response as the callback.

response checks the result object for errors and prints the outcome.

This code retrieves the values set in the previous example.

request sends a request for the sample_time and sample_rand for VIEWER and specifies response as the callback.

response obtains the VIEWER's values for these fields and outputs them.

Working with ActivityStreams

This example puts an entry into your ActivityStream.

The loadData function sends a request for the VIEWER and specifies request as a callback.

request opens a new ActivityStream and posts an activity where the title contains the VIEWER's name and the current time, specifying response as the callback for the request. When this function executes, the user will be prompted to give this application permission to post to their ActivityStream.

response checks to see if an error code was returned from the new activity request.

This example will obtain your ActivityStream and print its contents.

The request function fetches the VIEWER's ActivityStream and specifies response as the callback.

response iterates over each returned activity and prints the title of the activity.

You are in profile mode. This code shows how to make a link to the canvas mode.

Note that the following method gets the Application ID from an ActivityStream request, and an Orkut profile UID from the OWNER's Profile URL. This approach is a temporary workaround, and functions to generate links will be availble in an upcoming version of the API.

]]>