JavaScript API
Contents |
Introduction
The StatusNet JavaScript API is a (nearly) complete JavaScript wrapper for the StatusNet API. The implementation is Open Source just like StatusNet itself. The goal is to allow developers who want a deeper level of integration / interaction with StatusNet to build their business logic without having to think about the lower level code needed to authenticate and communicate with a StatusNet instance. The JavaScript API also powers the StatusNet Social Widgets.
The StatusNet JavaScript API and Social Widgets were inspired by Twitter's @Anywhere API, and were originally created as a 2010 Google Summer of Code (GSoC), by Arunoda Susiripala @arunoda.
Features
- JavaScript wrapper around the StatusNet API
- Easy authentication
- Event Handling System
Repository
The code can be found here: https://gitorious.org/+statusnet-developers/statusbox/statusnet-developers-statusbox
How to use it
- Install the JavaScript API Plugin for the instance of StatusNet you wish to use the API with.
- Register an OAuth client application (Note: The API uses OAuth for authentication; HTTP basic authentication is not supported.)
- Load JQuery. The JavaScript API heavily leverages JQuery. JQuery must be loaded to use the API. -- JQuery v.1.4.2 or later
- Configure and load the JavaScript API for your particular StatusNet instance.
Load and configure the API in your web page
<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js ></script>
<script type="text/javascript" src='http://example.net/js/api.min.js'></script>
<script type='text/javascript'>
SN.init('YOUR_OAUTH_CONSUMER_KEY', 'YOUR_OAUTH_CONSUMER_SECRET');
</script>
Example Application
After you've loaded the JQuery and the JavaScript API, your're ready to access StatusNet API resources. The following example shows the standard application structure.
Note: SN is the standard global namespace used by the JavaScript API.
// Intialize the JavaScript API with your OAuth client info
SN.init("YOUR_OAUTH_CONSUMER_KEY", "YOUR_OAUTH_CONSUMER_SECRET");
// Once the API is loaded you can
SN.ready(function() {
if (SN.isConnected()) {
// Get connected user's home timeline
// notices is an array of notices
SN.User.current().homeTimeline().ready(function(notices) {
// Output the notices here
// TODO: show more complete example code
});
} else {
// For non-connected users we draw the login button like this:
$('#login-button').html('<button onlick="SN.connect();">Login</button>');
}
});
API Usage
StatusNet JavaScript API is compatible with twitter's @Anywhere API and we've implemented its
*User *CurrentUser *Status *DirectMessage
classes completely. And additionally we provide a simple Search class which is native to us. The reason to follow the twitter API is it'll ease the learning curve for developers.
All of the above classes are tied with the SN global object. eg:
SN.User contains the User Class.
- All of the API classes are using method chaining and callbacks to perform their functionality.
- After you've performed method chaining you call the ready() method with a callback function.
- Here is a Sample of getting a list of followers for a given user.
SN.User.find('arunoda').followers().ready(callbackFunc);
function callbackFunc(followers){
//followers contains array of Users
//You can use them accordingly.
}
Instead of giving a callback function as a separate function, you can give it online as we are doing with JQuery
SN.User.find('arunoda').followers().ready(function(followers){
//followers contains array of Users
//You can use them accordingly.
});
The very same style follows for other classes too..