OpenSocial is a set of APIs for building social applications that run on the web. OpenSocial's goal is to make more apps available to more users, by providing a common API that can be used in many different contexts. Developers can create applications, using standard JavaScript and HTML, that run on social websites that have implemented the OpenSocial APIs. These websites, known as OpenSocial containers, allow developers to access their social information; in return they receive a large suite of applications for their users.
The OpenSocial APIs expose methods for accessing information about people, their friends, and their data, within the context of a container. This means that when running an application on Orkut, you'll be interacting with your Orkut friends, while running the same application on MySpace lets you interact with your MySpace friends. For more information on the types of information exposed by the OpenSocial API, see the Key concepts section.
This document describes the concepts and principles behind the OpenSocial JavaScript APIs. It is a complement to the JavaScript API Reference, which covers the details of specific methods and parameters. This document does not describe any particular implementation of the API.
So what does it mean to be an OpenSocial container? In a practical sense, it means that a site can run any application built on the OpenSocial APIs. This implies a number of things:
Methods may return the error code opensocial.ResponseItem.NOT_IMPLEMENTED for a specific method if the container does not support a specific request. No additional public methods or classes may be put on the OpenSocial namespace at any level. The required methods and fields are defined in the following JavaScript files:
Extra person, activity or other object fields should be defined in an enum under the container's namespace, and the environment should allow applications to discover these fields. For example, if the field orkut.PersonField.SPECIAL_FIELD is defined as "orkut.specialPersonField", then opensocial.getEnvironment().supportsField("person", "orkut.specialPersonField") and opensocial.getEnvironment().supportsField(opensocial.Environment.ObjectType.PERSON, orkut.PersonField.SPECIAL_FIELD) should both return true.
Any extra data request types should be available with a namespaced call like myspace.newFetchAlbumRequest. The gadget would then use batchRequest.add(myspace.newFetchAlbumRequest(...)).
Any extra objects may be added to the container's own namespace. These objects can be returned from person.getField or other similar requests.
In short, this requires handling three types of requests: the Gadget Rendering Request, the Gadget Metadata Request, and the JavaScript Request.
For the JavaScript APIs, you must implement the gadgets core JavaScript API, as per the documentation.
Containers must provide both JSON and AtomPub represenatations of data. OpenSocial uses HTTP GET to retrieve, PUT to update in place, POST to create new, and DELETE to remove. POST operates on collections and creates new activities, persons, or app data within those collections.
The rest of this document provides background information only and is not part of the OpenSocial specification. OpenSocial container compliance is determined solely based on the previous section, Compliance.
Social applications revolve around people and their relationships. OpenSocial provides a standard way for websites to expose their social graph and more. Seeing the activities of other people helps you stay up to date with your friends, and allows everything from resumes to videos to spread virally through the graph. OpenSocial also provides a way for application data to persist on a social networking site, as well as specifying the different ways that an application can be viewed within an OpenSocial container. Finally, OpenSocial defines an API through which users of web services, mobile devices, and desktop applications may interact with social data
It's people! Social graphs are made out of people! People are a fundamental part of social networking software and the OpenSocial API. The Person object provides access to a user's information. Part of this information is stored in the user's profile and, depending on the site, can include anything from "favorite TV shows" to "5 things you'll find in my bedroom." The other important user information is the set of connections they have in the social graph and this is covered in the Relationships section.
There are two Person objects that can be requested directly¿the VIEWER and the OWNER. To understand the distinction, imagine you're checking out a coworker's profile on Orkut. In this case, you are the VIEWER and your coworker is the OWNER. It's also common to view your own profile, in which case you are both the VIEWER and the OWNER, and some applications may choose to handle this case differently. OpenSocial also provides for the case of anonymous viewing, where the gadget will not be able to access the VIEWER's information.
The API Reference contains more detailed information about the Person class.
A note about user IDs
One of the pieces of data that is always returned with a Person object is the user's ID. The user ID must be alphanumeric (A-Za-z0-9) and must uniquely identify the user in a container. This standardization is intended to allow for prefixing IDs with a domain name and separator to create globally unique IDs (e.g. "orkut.com:34KJDCSKJN2HHF0DW20394"). Note that there will likely be a size limit placed on user IDs to help manage storing IDs in a database.
The ability to create relationships is what turns a multi-user application into social software. Being able to share information and interact with friends changes the dynamic of user experience¿you're engaging with people, not software.
There are two representations of sets of people available to OpenSocial¿VIEWER_FRIENDS and OWNER_FRIENDS. In the case where you're checking out a coworker's profile, requesting VIEWER_FRIENDS will return the set of users that are friends with you, while requesting OWNER_FRIENDS will return the set of users that are friends with your coworker. Logically, if you're viewing your own profile, VIEWER_FRIENDS and OWNER_FRIENDS will be the same set of users. Also, if the container support anonymous profile browsing, then the application won't be able to access the set of VIEWER_FRIENDS.
Note that OpenSocial makes no assumptions about the relationship between VIEWER and OWNER. The VIEWER and OWNER could be friends, but if you're looking at a stranger's profile, there's no relationship between you, the VIEWER, and them, the OWNER.
When an application wants to interact with or display data for "friends of friends", the OpenSocial specification supports extending a query for VIEWER_FRIENDS or OWNER_FRIENDS by a NETWORK_DISTANCE parameter. Containers can optionally support "friends of friends" queries, "friends of friends of friends" queries, and so on.
Since we can't be online all the time, it helps to have a record of what our friends have been up to, so long as your friends want to share that record. Seeing how other people are interacting with a social application also allows you to learn new features and uses of the application, so activity streams are one of the major drivers for organic growth of applications.
OpenSocial exposes activity streams, which are a collection of actions a user has taken in the context of a given container. These activities can include interaction with the container itself, such as updating your profile or installing a new gadget, or interaction with an OpenSocial application, such as sending your friend a virtual gift or setting a new high score in a game.
Activity templates allow application developers to define messages with placeholders for pieces of applicaton or user data. This separation of data and presentation allows multiple activities to be combined into activity summaries¿consolidated bundles of activities that let users know what their friends are up to without having to wade through a flood of messages.
The API Reference contains more detailed information about the Activity class.
Applications can provide a richer user experience if they can save their state between sessions. OpenSocial defines a data store that applications can use to read and write user-specific data. This data store can be read by anyone who can see the gadget, but only the VIEWER's user-scoped data is writable.
Clearly this free data store could be abused, so containers may implement quotas or rate limits to preserve their disk space. However, OpenSocial does not currently define these policies.
The keys that developers specify to index this data must only contain alphanumeric (A-Za-z0-9) characters, underscore(_), dot(.) or dash(-).
Since application data is often created based on user inputs, OpenSocial containers perform automatic HTML escaping of all application data. However, developers have the option of turning off this escaping by setting the escapeType parameter on newFetchPersonAppData and getField calls. See the Persistence section of the OpenSocial Developer's Guide for more details.
Containers can support several different locations where gadgets may be rendered. Such locations are formally called views (formerly Surfaces in earlier versions of the API). All gadgets (not just OpenSocial applications) have become view-aware. A gadget can ask which view it is currently being rendered on, as well as what views the container supports.
Containers may define their own views, but the specification defines the following standard views in the gadgets.views.ViewType object:
In addition to requesting data about the current view, gadgets may also request that the container navigate the user to another view. Imagine a news gadget that normally runs in a small profile view that changes to a full canvas view if the user clicks on a headline to get more information. Gadgets can choose the best rendering context for their current state.
When navigating to another view, a gadget may also request a change in OWNER. For example, imagine an application that places a survey on the user's profile view. This application may want to provide links to the user's friends' profiles, so that the user can see which surveys his or her friends are currently displaying.
Containers can also specify a template that applications can use to generate URLs to a specific application canvas and user. Such URLs can be shared via activity updates and printed on external websites, so linking to applications from external sources is easy.Certain types of events may occur during the lifecycle of a gadget that are desirable yet difficult for developers to track. Such events include installs, uninstalls, rate limiting, directory listing changes, and blacklist/whitelist notifications. The gadget specification allows developers to specify URLs that the container will POST event data to when certain events are triggered.
By tracking the data POSTed to the specified URL, gadget developers can accurately track number of installs, remove database entries upon uninstall, and get automatic notifications if their gadget exceeds a quota or is marked as spam by the container. A container may optionally support sending lifecycle events to an application developer's site by sending relevant query parameters to a URL endpoint. To receive these events, you can place one or moreLink tags
in your application's XML. Each Link tag has a rel and href attribute. The
href attribute denotes the endpoint where event pings are sent. If the
rel attribute is "opensocialevent", then all events are sent to that endpoint.
If the rel attribute matches "opensocialevent.TYPE", then events of TYPE are sent
to that endpoint. An optional method attribute can be set to POST or
GET to specify how the request should be sent. The default is GET.
<Link rel="event" href="http://www.example.com/pingme" method="POST/> <Link rel="event.addapp" href="http://www.example.com/add" /> <Link rel="event.removeapp" href="http://www.example.com/remove" /> eventtype - the event type Note that these requests will always be signed by the container, and your server should check that the signature is valid by using the OAuth algorithm for signature verification.
Most events will have information about one or more OpenSocial ID
values. These id values are passed as one or more id attributes.
Note that a single ping may aggregate a number of events by specifying
many id values.
The following event types are defined. Container custom events should
be designated with a domainname prefix to avoid namespace collisions.
| Event | Description |
|---|---|
addapp |
id attribute identifies users who have installed the
app.from attribute designates how the user added
this app. Values are "invite", "gallery", "external".
Optional. |
removeapp |
id attribute identifies users who have removed the
app. |
app |
action attribute designates the action performed on the
app. Values are "enabled", "disabled", and "approved". reason attribute identifies the reason for the action.
Values are "policy", "quota", "maintenance". Optional. |
invite |
id attribute indicates users who have been invited.from_id attribute indicates the user who sent the invitation. |
When is an OpenSocial application not just a gadget? When it has a backend server that talks directly to the container, of course! The OpenSocial RESTful API specification defines a way for servers, mobile devices, and desktop computers to interact with an OpenSocial container without requiring JavaScript or direct user interaction. Applications can update activities, send messages, and fetch OpenSocial data in the background using either an AtomPub or JSON REST service.
This section describes several common patterns in the OpenSocial API. Understanding these concepts should help you wade through the API Reference.
The OpenSocial API queries the container asynchronously for most of its calls, which is why most OpenSocial methods don't directly return data, but rather allow you to specify a callback that will be executed when the server's response is ready. Of course, making lots of asynchronous requests isn't always ideal, so the API allows for batch requests to allow developers to ask for many pieces of information at once. A developer can create an opensocial.DataRequest and add several individual request objects to it. Upon receiving the DataRequest, the container can process each request optimally and return the results of each operation as a batched result object. Containers must preserve the semantics of executing requests in serial order, though. A request that contains a write and then a read must return the newly written data, while a request that contains a read and then a write must return the data that was present before the write took place.
The gadgets and OpenSocial specifications determine the
common APIs that all containers will support, but there are cases where a
certain method or a profile field will be offered as an extension in some
containers. To help developers write gadgets that can take advantage of these
extensions, yet degrade gracefully in their absence, these APIs include
gadgets.util.hasFeature and
opensocial.Environment.supportsField methods to query the
container at runtime and determine which features are available.
Of course, OpenSocial has compliance rules that must be adhered to by containers wishing to extend the API.
There are cases where a gadget may wish to perform an action
that needs approval by the user or mediation by the container. OpenSocial
supports "request" features that allow the container to decide how to handle
the interaction with the user. Functions like
opensocial.requestCreateActivity, and
opensocial.requestPermission allow the container to inject its own
policy decisions into the gadget execution flow and notify the gadget of the
result. Under this specification, it is equally valid for a container to defer
to the user, always approve, always deny, or use any other method to determine
responses to request calls. Additionally, this allows the container to enforce
UI flows in a safe and integrated way.
User profiles are different from container to container. On consumer-oriented containers, applications may be interested in a user's idea of a perfect first date, but this may not be appropriate for business-oriented containers. This is why OpenSocial allows containers to extend Person objects with arbitrary fields¿to match the feel and uses of each container site. Profile fields are therefore obtained through Person.getField calls rather than having hardcoded getters and setters for each property. See the Compliance and Capabilities discovery sections for more information about how containers can add additional data to the API, and how applications can determine which fields are available for use.
Gadgets can use the gadgets.io.makeRequest
method to pull HTML, JSON, and ATOM data from remote servers.
The gadgets.io.makeRequest call can also be used to pass data from gadgets back to application servers in a way that cannot be spoofed. The container is expected to mediate communications from the gadget to the application server (e.g. ilike.com). Therefore trusted content fetching has two main steps: [1] the gadget contacts the container, and [2] the container contacts the application server.
makeRequest call supports full OAuth authentication via a container proxy. Gadgets can now access protected web services without requiring the user to share their password with the gadget, allowing for sophisticated and secure integration with web APIs that support OAuth.When gadgets call makeRequest, the
calls may be cached. This is great for reducing the load that your servers
have to be able to handle, since OpenSocial applications may have millions of
users, each requesting data from your server. However, this caching of
makeRequest calls can wind up displaying old data to users if the content
returned by your server changes often.
If you expect the content at the URL you are fetching
to change frequently, you may want to work around the automatic caching with
a more finely grained method. You can define a refreshInterval parameter, which
should be specified as the number of seconds that should elapse before
the makeRequest call requests content from your servers again.
For example, specifying 10 will mean that the container would only query the
supplied URL once every 10 seconds, no matter how many people are using the
application. Specifying 0 will query the server each time the function is
called, effectively bypassing the cached version.
The JavaScript API reference is a complete listing of the methods and fields that OpenSocial requires, but some conventions are not immediately understandable. This section will explain some of the assumptions used in generating the API reference.
JavaScript is not strongly typed, but in order to make the developer experience consistent, the APIs are defined as if JavaScript supported strongly typed data. Cases where a parameter or return value are defined as a certain type in the documentation indicate that this is the only type that can be provided or accepted for OpenSocial compatibility. Any exceptions to this rule will be directly documented in the reference.
As an example, the getErrorCode method is defined in the JavaScript API reference as returning a value of the type opensocial.ResponseItem.Error, although in practice this value will be a normal JavaScript string. However, developers can be assured that in a compliant container, the string returned is one of the opensocial.ResponseItem.Error fields.
Enumerations figure heavily into the OpenSocial API. Developers are permitted to reference an enum or use the enum's value directly. To avoid collisions related to extensibility, containers must ensure that the names and values for an enum match the ones defined in the spec and that custom values are namespaced appropriately. For example, an Orkut custom field named MY_PERSON_FIELD could have a name of Orkut.PersonField.MY_PERSON_FIELD and a value of "Orkut.myPersonField". See the Compliance and Capabilities discovery sections for more information.
Many of the OpenSocial methods have a large number of optional parameters. JavaScript's lack of strong typing makes overloading difficult at best, and having functions with a large amount of parameters can be difficult to manage (especially for developers not using a nice IDE). OpenSocial uses the "object bag" approach to optional parameters¿many methods take an additional opt_params parameter that takes the form of a map of parameter names to their corresponding values.
For each method which uses an opt_params map the spec defines all of the valid fields that will be read out of the map by the
container. These fields have defined types as discussed in the Virtual typing section. If a developer adds an
invalid value for a field in the map the container should return the BAD_REQUEST error code.
The copyrights in this specification are licensed under the Creative Commons Attribution 2.5 license | Terms of Service