Introduction to OAuth documentation

I have manuallly written this documentation using jquery and html.

Download oauth.js from my site and include it in your application.

Create and object of OAuth as
var OAuth = oAuth();

Refer to rest of the documentation for different methods and it's usage

setConfig(config,callback)

This should always be the first method invoked on oauth object. Following is the syntax of a typical configuration which must be passed to this method.

		var config={ consumerKey   : "vXocyjnewewee2PGQwJyQw"
					, consumerSecret: "yltqSptG6HIb2a0qOFfIhkewewepBvwGFjU4"
					, serviceProvider:
					  { 	signatureMethod     : "HMAC-SHA1"
						, requestTokenURL     : "http://twitter.com/oauth/request_token"
						  , userAuthorizationURL: "http://twitter.com/oauth/authorize"
						  , accessTokenURL      : "http://twitter.com/oauth/access_token"
						  
					  },
					appDomain:"http://akshar.co.in/"
				};
		var oauth=oAuth();
		oauth.setConfig(config,helper_function);
		  

The second argument is a callback method invoked with two arguments

	function helper_function(oauthTokenSecret,oauthToken)
	{
		save("oauth_token_secret",oauthTokenSecret); //Save it somewhere for future use
		save("oauth_token",oauthToken); //Save it somewhere for future use
		gBrowser.addTab("http://twitter.com/oauth/authorize?oauth_token="+oauthToken); //This is most common step
	}
		

Signing Messages

Here is a small peice of code that describes how one can sign messages and send them post authentication using oAuth.

Prepare your message to be sent over AJAX

var accessor = {
				consumerSecret : "ConsumerSecret", //Both these are provided by service provider. In our case Twitter. 
				tokenSecret : "TokenSecret"
			};

var message = {
			action : "http://twitter.com/statuses/timeline.json",
			method : "GET",
			parameters : [
				["oauth_consumer_key",ConsumerKey],
				["oauth_token", getSavedValue("oauth_token")], // This value is obtained from Authenticate function!
				["oauth_signature_method",signatureMethod], //Mostly HS-SHA1
				["oauth_version","1.0"]
			]
		};
var OAuth = oAuth();
		
OAuth.setTimestampAndNonce(message);
OAuth.SignatureMethod.sign(message, accessor);


var oAuthArgs = OAuth.getParameterMap(message.parameters);
var authHeader = OAuth.getAuthorizationHeader("http://twitter.com/", oAuthArgs);

Once we have signed the message we can send the HTTP request as below. The headers must be specified.

var req = new XMLHttpRequest();
req.mozBackgroundRequest = true;
req.open(message.method, message.action, true);
req.setRequestHeader("Authorization", authHeader);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-Length", argstring.length);
			

Introduction to jsTwitt Library

I have manuallly written this documentation using jquery and html.

Download jsTwitt.js from my site and include it in your application.

There is no need to instantiate anything. You have available a variable jsTwitt which should be used to invoke all other methods. It relies on callbacks for returning values. A typcial function would look like

jsTwitt.trends(function(trends){ alert(trends.trends.length); // alerts the number of trends returned. });

Refer to rest of the documentation for different methods and it's usage. Please note that this API is dependent on oAuth.js library

setConfig(config)

This method is used to configure the twitter api to the user's settings.

var config= { 
			  consumerKey   : "vXocyjnerwrwJyQw"
			, consumerSecret: "yltqSptG6HIb2a0werwertGlrL2rAbiEpBvwGFjU4"
			, serviceProvider:
				{ 	signatureMethod     : "HMAC-SHA1"
				, requestTokenURL     : "http://twitter.com/oauth/request_token"
				, userAuthorizationURL: "http://twitter.com/oauth/authorize"
				, accessTokenURL      : "http://twitter.com/oauth/access_token"

				},
			appDomain:"http://akshar.co.in/"
		};
jsTwitt.setConfig(config);

authenticate(callback)

This method must be called after calling setConfig method. The callback method is called only if the authentication was successful.

var config= { 
			  consumerKey   : "vXocyjnerwrwJyQw"
			, consumerSecret: "yltqSptG6HIb2a0werwertGlrL2rAbiEpBvwGFjU4"
			, serviceProvider:
				{ 	signatureMethod     : "HMAC-SHA1"
				, requestTokenURL     : "http://twitter.com/oauth/request_token"
				, userAuthorizationURL: "http://twitter.com/oauth/authorize"
				, accessTokenURL      : "http://twitter.com/oauth/access_token"

				},
			appDomain:"http://akshar.co.in/"
		};
jsTwitt.setConfig(config);
jsTwitt.authenticate(function(){
	alert("Authentication Successful");
});

setValue(key,value)

This method must be extended by user. By default it uses firefox's preference store. But user may chose any other way. The idea is only to provide a storage option to the api. Ideally if user wants to override these methods it should be done before calling the authenticate method. These methods will never be used by user directly. They are used internally by the API.

jsTwitt.setValue=function(key,val){ writeCookie(key,val); }

getValue(key)

This method must be extended by user. By default it uses firefox's preference store. But user may chose any other way. The idea is only to provide a storage option to the api. Ideally if user wants to override these methods it should be done before calling the authenticate method. These methods will never be used by user directly. They are used internally by the API.

jsTwitt.getValue=function(key,val){ readCookie(key,val); }

statusUpdate(msg,callback)

This method maps to Twitter's statuses/update call. The callback function is optional.

jsTwitt.statusUpdate("Hello World",function(){ alert("status updated"); });

statusMentions(callback)

This method maps to Twitter's statuses/mentions. The callback function has one argument as a the JSON object returned by twitter.

jsTwitt.statusMentions(function(mentions){ alert("Your name was mentioned "+mentions.length+"times"); });

statusUserTimeline(callback)

This method maps to Twitter's statuses/user_timeline. The callback function has one argument as a the JSON object returned by twitter.

jsTwitt.statusUserTimeline(function(timeline){ alert("Your tweets are: "+timeline.length); });

statusFriendsTimeline(callback)

This method maps to Twitter's statuses/friends_timeline. The callback function has one argument as a the JSON object returned by twitter.

jsTwitt.statusFriendsTimeline(function(timeline){ alert("Your friend's tweets are: "+timeline.length); });

statusHomeTimeline(callback)

This method maps to Twitter's statuses/home_timeline. The callback function has one argument as a the JSON object returned by twitter.

jsTwitt.statusHomeTimeline(function(timeline){ alert("Your home tweets are: "+timeline.length); });

statusShow(id,callback)

This method maps to Twitter's statuses/show. Given a id it returns the that particulat tweet. The callback function has one argument as a the JSON object returned by twitter.

jsTwitt.statusShow("1453333", function(tweet){ alert("This was tweeted at"+tweet.created_at); });

friendshipsShow(source_id,target_id,callback)

This method maps to Twitter's friendships/show. Given two people's id this method returns all the information about their relationship. The callback function has one argument as a the JSON object returned by twitter.

jsTwitt.friendshipsShow("14333","42323" function(tweet){ alert("First User is "+tweet[0].screen_name); });

trends(callback)

This method maps to Twitter's trends. It returns 10 latest trending topics.

jsTwitt.trends(function(trendst){ alert("We recieved "+trends.trends.length+" trends"); });

accountVerifyCredentials(callback)

This method maps to Twitter's account/verify_credentials. It returns the logged in user's information.

jsTwitt.accountVerifyCredentials(function(user){ alert("Welcome"+user.screen_name); });

destroySession()

This method destroys the stored token and hence results in the signing off of the user.

jsTwitt.destroySession();

Overlay.js [ATwitt Firefox Plugin code]

I have manuallly written this documentation using jquery and html.

Overlay.js can be found in atwitt/content directory in the source.

Refer to rest of the documentation for different methods and it's usage. Please note that this API is dependent on oAuth.js library

$(id)

Given an 'id' it returns the XUL element of that id

var msg = atwitt.$("tweet-textbox").value;

showTrend(node)

Given a node it searches for that particular trend by opening a new browser window.

getTrends(event)

This is an event handler for a click on "Trends" menu. It opens a small pop-up loaded with 6 latest trends from Twitter. The popup appears near the co-ordinates where the mouse click occured.

getTimeline(event)

This is an event handler for "Timeline" menu. It displays the timeline XUL panel.

postRequest (msg)

This method is used as an event handler to post the "Tweets". It internally calls jsTwitt.statusUpdate

getMentions

Similar to getTimeline. It internally calls jsTwitt.statusMentions

logoff

This method logs off a user if he is already logged in and vice versa.