Back to Top

libraries > Oauth.js

new Oauth(grantType, tokenKey)

Class containing methods to authenticate and make authenticated requests with a third party API

Parameters

Name Type Description
grantType string

Oauth grant type. One of: refresh_token, password, or custom.

tokenKey string

The key of the secret containing the oAuth token information.

Examples

// Refresh token flow.
const Oauth = require('vendor/Oauth.js');
const oauth = new Oauth('refresh_token', 'service.oauth');
// Username password flow.
const Oauth = require('vendor/Oauth.js');
// Save username and password from external standalone secrets.
let token = JSON.decode(Mesa.secret.get('service.oauth', '{}'));
token.username = Mesa.secret.get('service-username');
token.password = Mesa.secret.get('service-password');
Mesa.secret.set('service.oauth', JSON.stringify(token));
const oauth = new Oauth('password', 'service.oauth');
// Skubana flow (access_token that does not expire)
const Oauth = require('vendor/Oauth.js');
const oauth = new Oauth('custom', 'skubana.oauth');
const getResponse = oauth.get('https://dev.skubana.com/v1/orders');
Mesa.log.info('Get Response: ', getResponse);
// Oauth Usage
const getResponse = oauth.get('https://example.com/products.json');
const postResponse = oauth.post('https://example.com/products.json', {});
Mesa.log.info('Get Response: ', getResponse);
Mesa.log.info('Post Response: ', postResponse);

Returns

Void

Oauth.get(path[, options])

Make a GET request to an external Rest API

Parameters

Name Type Description
path string

Request path

options Options

Additional configuration for Oauth calls

Optional

Returns

object

Oauth.post(path, data[, options])

Make a POST request to an external Rest API.

Parameters

Name Type Description
path string

Request path

data object

Request payload

options Options

Additional configuration for Oauth calls

Optional

Returns

object

Oauth.put(path, data[, options])

Make a PUT request to an external Rest API.

Parameters

Name Type Description
path string

Request path

data object

Request payload

options Options

Additional configuration for Oauth calls

Optional

Returns

object

Oauth.patch(path, data[, options])

Make a PATCH request to an external Rest API.

Parameters

Name Type Description
path string

Request path

data object

Request payload

options Options

Additional configuration for Oauth calls

Optional

Returns

object

Oauth.delete(path[, options])

Make a DELETE request to an external Rest API.

Parameters

Name Type Description
path string

Request path

options Options

Additional configuration for Oauth calls

Optional

Returns

object

{Object} Options

Contents of an oAuth Token

A JSON-encoded secret with will automatically be set when you complete the oAuth authorization flow with the following contents:

Properties

Name Type Description
service string

The name of the service. google, dropbox, etc.

Optional
client_id string

The app in the service's client_id. For Mesa app authorization tokens, this will be empty.

Optional
client_secret string

The app in the service's client_client. For Mesa app authorization tokens, this will be empty.

Optional
authorization_token string

The toekn

Optional
refresh_token string

The token used to refresh the authorization_token. Only used in the refresh_token grantType flow.

Optional
token_url string

The url to call with the refresh_token to obtain a new authorization_token. Only used in the refresh_token grantType flow.

Optional
expires_at string

Seconds timestamp when the auhorization_token will expire.

Optional
username string

Only used in the password grantType flow.

Optional
password string

Only used in the password grantType flow.

Optional

{Object} Options

options parameter for Oauth calls

By default Oauth calls will auto-wrap any outgoing JSON data, eg. Oauth.post('/admin/products.json', data) will result in {'grant_type':'refresh_token','client_id':'1234','client_secret':'8904378947894'}

use skipJsonWrap=true to override this behavior

Properties

Name Type Description
query object

Parameters to append to the querystring.

Optional
headers object

Headers to send to the request.

Optional
debug=false bool

Log request information and response headers.

Optional
skipJsonWrap=false bool

Skip the named auto wrapping of outgoing data

Optional