Converting credentials to base 64 formats for the API call (Access token retrieval)

The function converts the string combining the username and password to base 64 format and apply in the API call for generating the access token.
Sample API request code snippet

let accessTokenresponse = https.post({
    url: AUTH_URL,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' + getBasicAuthorisation(credentialFetch),
    },
    body: {
        'grant_type': 'client_credentials',
    }
});



/**
 * @description Converting the username and password to base 64 format to sent as header
 * @param credentialFetch Object having the credentials
 * @returns {string} Base 64 formatted string value
 */
function getBasicAuthorisation(credentialFetch) {
    return encode.convert({
        string: credentialFetch.userName + ":" +credentialFetch.password,
        inputEncoding: encode.Encoding.UTF_8,
        outputEncoding: encode.Encoding.BASE_64
    })
}

Leave a comment

Your email address will not be published. Required fields are marked *