lib/api.js
function(callback) {
        var self = this;

        var auth_post_data = querystring.stringify({
            'grant_type': 'client_credentials'
        });

        return new Promise(function(resolve, reject){
            self.client.methods.authentication_oauth(_.extend({}, self.requestOptions, {
                data: auth_post_data,
                headers: _.extend({}, self.requestOptions.headers, {
                    'Authorization': _getBasicAuthHash(self.config.clientId, self.config.clientApiKey),
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': Buffer.byteLength(auth_post_data)
                })
            }), function(data) {
                // Authorization succeeded
                if (data.token_type && data.access_token) {
                    _.extend(self.requestOptions.headers, {
                        'Authorization': data.token_type + ' ' + data.access_token
                    });
                    // Multiplying expires_in (seconds) by 1000 since JS getTime() is expressed in ms
                    self.authorizationExpireTime = new Date().getTime() + ( data.expires_in * 1000 );
                    resolve(data);
                    if (_.isFunction(callback)) {
                        callback(data);
                    }
                } else {
                    reject(data);
                }
            }).on('error', function (err) { reject(err.message) });
        });
    }
function(callback) {
        var self = this;

        var auth_post_data = querystring.stringify({
            'grant_type': 'client_credentials'
        });

        return new Promise(function(resolve, reject){
            self.client.methods.authentication_oauth(_.extend({}, self.requestOptions, {
                data: auth_post_data,
                headers: _.extend({}, self.requestOptions.headers, {
                    'Authorization': _getBasicAuthHash(self.config.clientId, self.config.clientApiKey),
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': Buffer.byteLength(auth_post_data)
                })
            }), function(data) {
                // Authorization succeeded
                if (data.token_type && data.access_token) {
                    _.extend(self.requestOptions.headers, {
                        'Authorization': data.token_type + ' ' + data.access_token
                    });
                    // Multiplying expires_in (seconds) by 1000 since JS getTime() is expressed in ms
                    self.authorizationExpireTime = new Date().getTime() + ( data.expires_in * 1000 );
                    resolve(data);
                    if (_.isFunction(callback)) {
                        callback(data);
                    }
                } else {
                    reject(data);
                }
            }).on('error', function (err) { reject(err.message) });
        });
    }
function ( callback ) { var self = this ; var auth_post_data = querystring . stringify ( { 'grant_type' : 'client_credentials' } ) ; return new Promise ( function ( resolve , reject ) { self . client . methods . authentication_oauth ( _ . extend ( { } , self . requestOptions , { data : auth_post_data , headers : _ . extend ( { } , self . requestOptions . headers , { 'Authorization' : _getBasicAuthHash ( self . config . clientId , self . config . clientApiKey ) , 'Content-Type' : 'application/x-www-form-urlencoded' , 'Content-Length' : Buffer . byteLength ( auth_post_data ) } ) } ) , function ( data ) { // Authorization succeeded if ( data . token_type && data . access_token ) { _ . extend ( self . requestOptions . headers , { 'Authorization' : data . token_type + ' ' + data . access_token } ) ; // Multiplying expires_in (seconds) by 1000 since JS getTime() is expressed in ms self . authorizationExpireTime = new Date ( ) . getTime ( ) + ( data . expires_in * 1000 ) ; resolve ( data ) ; if ( _ . isFunction ( callback ) ) { callback ( data ) ; } } else { reject ( data ) ; } } ) . on ( 'error' , function ( err ) { reject ( err . message ) } ) ; } ) ; }
OAuth2 authorization mechanism. After authorization request, calls the callback with returned authorization data
@param {function}    callback
@returns {object} request promise
OAuth2 authorization mechanism . After authorization request calls the callback with returned authorization data