In this post, we'll look at how to directly call the Open Social API without going through a pre-existing gadget. Our motivation is the Open Social Activity API. Familiar social networking sites like Facebook and Twitter have "activity streams" in which each user posts status updates. These can also be automatically generated side effects (such as bookmarking a site with Del.icio.us's Twitter interface, for example).
An Open Social Activity API example using Google Gadgets is here: http://code.google.com/apis/opensocial/docs/0.8/devguide.html#Activities. In addition to being an HTML gadget, this relies on the Gadget API (see the call to gadgets.util.registerOnLoadHandler(startActivity) near the bottom). To do the same thing with Google Friend Connect, we need to take the following steps:
- Load the FriendConnect JavaScript libraries with google.load.
- Use the FriendConnect libraries to initialize an OpenSocial API call.
- Pass in a callback function (startActivity() in our example) that the server side (at Google HQ, presumably) will invoke.
- Implement the callback function using the OpenSocial Activity API.
<html>
<!--Shows how to use the Open Social activity API with Google Friend Connect -->
<body>
<div id="content_div"></div>
<!-- Initialize the Google Friend Connect OpenSocial API. -->
<!-- Load the Google AJAX API Loader -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<!-- Load the Google Friend Connect javascript library. -->
<script type="text/javascript">
google.load('friendconnect', '0.8');
</script>
<script type="text/javascript">
/* This will dynamically change the content to the new activity */
var div=document.getElementById("content_div");
/*No caching for development. */
google.friendconnect.container.setNoCache(1);
/* Use this to invoke Open Social methods */
google.friendconnect.container.initOpenSocialApi({
site: '##########################', /* Replace this with your GFC ID */
onload: function(securityToken) { startActivity(); }
});
/* This is a callback invoked when we initial the Open Social libraries. It has a callback method of its own, called callback() */
function postActivity(text) {
var params = {};
params[opensocial.Activity.Field.TITLE] = text;
var activity = opensocial.newActivity(params);
opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, callback);
div.innerHTML = "Activity title is: " + activity.getField(opensocial.Activity.Field.TITLE);
};
/**
* Server calls this function to indicate whether the activity post succeeded or failed.
*/
function callback(status) {
if (status.hadError())
{
alert("Error creating activity.");
}
else
{
alert("Activity successfully created.");
}
};
/**
* Start the process of posting an activity.
*/
function startActivity() {
postActivity("This is a sample activity, created at " + new Date().toString());
};
</script>
</body>
</html>
To use this, just save it as an HTML file on a Web server registered to Google Friend Connect and load the page. You will need to change "################" to use your registered site ID.
No comments:
Post a Comment