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
- config object
The following config parameters can be set.
Parameter | Type | Required | Default | Notes |
---|---|---|---|---|
consumerKey | string | true | ||
consumerSecret | string | true | ||
callbackUrl | string | oob | Sets the callback url for authorisation. OOB is used for PIN based auth |
|
accessTokenKey | string | Some services give you an access token so you can skip the OAuth dance |
||
accessTokenSecret | string | Required if 'accessTokenKey' was set | ||
signatureMethod | string | HMAC-SHA1 | jsOAuth only supports HMAC-SHA1 | |
realm | string | |||
requestTokenUrl | string | Required for 3-legged requests | ||
authorizationUrl | string | Required for 3-legged requests | ||
accessTokenUrl | string | Required for 3-legged requests | ||
boolean | false | Deprecated Allows you to put Firefox into a Cross domain requests enabled mode* |
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
- url string
- success function
- 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
- url string
- data object
- success function
- 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
- url string
- success function
- 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
- url string
- data object
- success function
- 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
- options object
Parameter | Type | Required | Default | Notes |
---|---|---|---|---|
method | string | GET | ||
url | string | true | ||
data | object | |||
headers | object | |||
success | function | |||
failure | function |
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
- success function
- 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
- 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
- success function
- 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
- tokenKey string
- tokenSecret string
Usage
1 oauth.setAccessToken('MY-ACCESS-KEY', 'MY-ACCESS-SECRET');