Acquiring oauth2 token

We are going to use NodeJs to acquire oauth2 token.

In your code editor create a file called app.js.   Copy and paste the following code in it.

// We will use axios to send http request
const axios = require('axios');

// We use query-string to parameterize object
const qs = require('query-string');

// Our api base url for all api calls
const base_url = 'https://api.clearclouds.ca/ns-api

// We build a token query parameters with your credentials
const query = {
grant_type : 'password',
username : 'yourapiusername',
password : 'yourapipassword',
client_id : 'your client id',
client_secret : 'your client sercret'
}

// create a token function for sending a post request
function getToken(){
return axios.post( base_url + '/oauth2/token?' + qs.stringify(query));
}
  

We will call the getToken function and handle the results.   Copy the code below and paste it just under our getToken function

getToken().then( response => {

// If the call is a success, our response will return an object with our token along with other values.
// We will simply just log out the token
console.log( response.data )

}).catch( error => {

// if we have any errors
console.log('Http status code: ', error.response.status );
console.log('Http response text: ', error.response.statusText);
console.log('http header warning: ', error.response.headers['warning'])

});

If everything is a success then our results will be something like this.  Otherwise, check the http status code, response and header warning.

{ access_token: '253f5c32ca2535441265f0b937341e7f',
  expires_in: 3600,
  scope: 'Super User',
  token_type: 'Bearer',
  refresh_token: 'e43334ef0b8c67ded5c77bf3befdb74a',
  legacy: false,
  domain: '0000.ClearClouds',
apiversion: 'Version: 40.1.3' }

Please note that this token is only valid for 3600 seconds in which the token is only valid for 60 minutes.

Now that we have our token,  let's continue on with how we would use the token to make additional api calls.

Let's create a function called getUserInfo which will give us info about a specific user.  This function will take in a token,  user extension and the domain as parameters.  Copy the code below and paste it just below our previous code.

function getUserInfo(token, user, domain){
// Our api call requires the token to be passed as bearer to our http post
const authorization = { authorization : 'Bearer ' + token };

// Build our http query
const query = {
object : 'subscriber',
action : 'read',
user,
domain
}

// Send the http request and return the response
return axios({
url : 'base_url' + '?' + qs.stringify(query),
headers : authorization
})
}

Now that our function is ready, we need to call this function inside our getToken().then( .... )right after console.log( response.data ) paste the following code.

getUserInfo( response.data.access_token, '<ext>', '<domain>' )
.then( user => console.log( user ) )
.catch( error => console.log( error.message ) )

Once you have executed node app.js, your results should have something similar below.

[ { dir: '334',
    user: '<redacted>',
    domain: '<redacted>',
    message: '',
    vmail_provisioned: 'yes',
    accept: 'yes',
    reject: 'yes',
    screen: 'yes',
    no_answer_timeout: '25',
    first_name: 'John',
    last_name: 'de Guerto',
    group: 'Office',
    site: 'n/a',
    subscriber_login: '<redacted>',
    subscriber_pin: '<redacted>',
    language: 'en_us',
    data_limit: '10000',
    call_limit: '10',
    time_zone: 'Canada/Eastern',
    dir_anc: 'yes',
    directory_order: '1',
    vmail_greeting: '1',
    vmail_enabled: 'yes',
    rej_anony: 'no',
    vmail_annc_time: 'no',
    vmail_annc_cid: 'no',
    vmail_sort_lifo: 'yes',
    vmail_fwd_to: '',
    vmail_notify: 'attsave',
    email: '<redacted>',
    ntfy_missed_call: 'no',
    ntfy_data_limit: 'yes',
    dial_plan: '<redacted>',
    dial_policy: 'Permit All',
    callid_nmbr: '<redacted>',
    callid_name: '<redacted>',
    callid_emgr: '<redacted>',
    area_code: '905',
    presence: 'open',
    dir_list: 'yes',
    date_created: '2019-02-06 19:07:47',
    srv_code: '',
    scope: 'Call Center Supervisor',
    last_update: '2020-02-01 15:32:35',
    gauSession: '0',
    pwd_hash:
     '<redacted>',
    vmail_transcribe: 'google',
    account_status: 'standard' } ]