API Reference

jsOAuth is a Javascript implimentation of the OAuth protocol.

OAuth

Creates an OAuth object which signs and makes requests. You must so this first, as all other calls depend on the object method creates.

Parameters

  1. config object

The following config parameters can be set.

ParameterTypeRequiredDefaultNotes
consumerKeystringtrue  
consumerSecretstringtrue  
callbackUrlstring oob

Sets the callback url for authorisation. OOB is used for PIN based auth

accessTokenKeystring  

Some services give you an access token so you can skip the OAuth dance

accessTokenSecretstringRequired if 'accessTokenKey' was set  
signatureMethodstring HMAC-SHA1jsOAuth only supports HMAC-SHA1
realmstring   
requestTokenUrlstringRequired for 3-legged requests  
authorizationUrlstringRequired for 3-legged requests  
accessTokenUrlstringRequired for 3-legged requests  
enablePrivilegeboolean falseDeprecated Allows you to put Firefox into a Cross domain requests enabled mode*
* Should only be used for testing

Usage

1 var config = {
2     consumerKey: "MY-KEY",
3     consumerSecret: "MY-SECRET"
4 };
5 
6 var oauth = OAuth(config);

get

Performs a GET request

Parameters

  1. url string
  2. success function
  3. failure function

Usage

1 function success(data) {
2 	alert('Success ' + data.text);
3 }
4 
5 function failure(data) {
6 	alert('Something bad happened! :(');
7 }
8 		
9 oauth.get('http://www.example.com/person/1', success, failure);

post

Performs a POST request

Parameters

  1. url string
  2. data object
  3. success function
  4. failure function

Usage

 1 function success(data) {
 2 	alert('Success ' + data.text);
 3 }
 4 
 5 function failure(data) {
 6 	alert('Something bad happened! :(');
 7 }
 8 
 9 var data = {
10 	name: 'Darth Vader',
11 	age: 43
12 };
13 		
14 oauth.post('http://www.example.com/person/edit/1', data, success, failure);

getJSON

Performs a GET request and parses JSON. Requires a JSON library

Parameters

  1. url string
  2. success function
  3. failure function

Usage

1 function success(data_object) {
2 	alert('Name: ' + data_object.name);
3 }
4 
5 function failure(data) {
6 	alert('Something bad happened! :(');
7 }
8 		
9 oauth.getJSON('http://www.example.com/person/1', success, failure);

postJSON

Performs a POST request and parses JSON. The post data is stringified using JSON.stringify, then posted as the request body. Requires a JSON library

Parameters

  1. url string
  2. data object
  3. success function
  4. failure function

Usage

 1 function success(data_object) {
 2 	alert('Name: ' + data_object.name);
 3 }
 4 
 5 function failure(data) {
 6 	alert('Something bad happened! :(');
 7 }
 8 		
 9 oauth.postJSON('http://www.example.com/person/1', {
10 	'name': 'Luke Skywalker',
11 	'age': '18'
12 }, success, failure);

request

Performs a request based on the configuration you give. More flexible than the previous request methods as you can specify additional headers.

Parameters

  1. options object
ParameterTypeRequiredDefaultNotes
methodstring GET 
urlstringtrue  
dataobject   
headersobject   
successfunction   
failurefunction   

Usage

 1 function success(data) {
 2 	alert('Name: ' + data.text);
 3 }
 4 
 5 function failure(data) {
 6 	alert('Something bad happened! :(');
 7 }
 8 
 9 var options = {
10 	method: 'POST';
11 	url: 'http://www.example.com/person/edit/2',
12 	success: success,
13 	failure: failure,
14 	headers: {
15 		'X-Do-Not-Track': 1
16 	},
17 	data: {
18 		'name': 'Luke Skywalker',
19 		'age': '18'
20 	}
21 };
22 		
23 oauth.request(options);

fetchRequestToken

Gets the request token from the OAuth service URL specified with the requestTokenUrl finally passing authorizationUrl and the request token string to the success callback.

Parameters

  1. success function
  2. failure function

Usage

1 oauth.fetchRequestToken(function (url) {
2 	var windowObjectReference = window.open(url, 'authorise');
3 }, function (data) {
4 	console.log(data)
5 });

getVerifier

Get the verifier that was previously set in this session

Usage

1 var pin = oauth.getVerifier();

setVerifier

After you open the authorisation window, you need to get the user to input the PIN givent to them by the service. You then set it with the setVerifier() method.

Parameters

  1. verifier string

Usage

1 var code = document.getElementById('verification').value;
2 oauth.setVerifier(code);

fetchAccessToken

Gets the access token from the OAuth service URL specified with the accessTokenUrl. The access token is parsed and stored. A call to getAccessToken() can retrive the parsed token.

Parameters

  1. success function
  2. failure function

Usage

1 oauth.fetchAccessToken(function (data) {
2 	console.log('Authorised for 3-legged requests from now on');
3 }, function (data) {
4 	console.log(data)
5 });

getAccessToken

Gets the stored access token

Usage

1 var accessToken = oauth.getAccessToken();

getAccessTokenKey

Gets the stored access token key

Usage

1 var accessTokenKey = oauth.getAccessTokenKey();

getAccessTokenSecret

Gets the stored access token secret

Usage

1 var accessTokenSecret = oauth.getAccessTokenSecret();

setAccessToken

Sets an access token for signing requests

Parameters

  1. tokenKey string
  2. tokenSecret string

Usage

1 oauth.setAccessToken('MY-ACCESS-KEY', 'MY-ACCESS-SECRET');