Authorize Using JavaScript

In order to use the EnterWorks APIs, you must first create an authorization token using the method below or by generating a token from the Swagger pages.

Use the following code snippet to obtain an authorization token using the JavaScript in a .jsp or .html file:

var uname = document.getElementById("username").value;
var pswd = document.getElementById("pswd").value;
var langExt = document.getElementById("langExt").value; -- optional for default language
var base_token ="Basic ZXBpbS1jbGllbnQ6ZXBpbS1jbGllbnQ=";
// e.g. http://epimhost:epimport
var host = window.location.protocol + "//" + window.location.hostname+ (window.location.port ? ':' + window.location.port: '');
// e.g. http://epimhost:epimport/webcm/rest/oauth/token?allowexpire=true&grant_type=password&language=langExt
var url = host + "/webcm/rest/oauth/token?allowexpire=true&grant_type=password&language="+langExt;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST',url,true);
xmlhttp.setRequestHeader("Authorization", base_token);
xmlhttp.setRequestHeader("username", escape(username));
xmlhttp.setRequestHeader("password", escape(password));
xmlhttp.send(null);
xmlhttp.onreadystatechange = function()
{
var retString = "";
if (xmlhttp.readyState == 4)

{
if ( xmlhttp.status == 200)
{
var answer = eval( "(" + xmlhttp.responseText + ")");
retString = "access_token: " + answer.access_token + "";
}
else
{
retString = "Error ->" + xmlhttp.responseText;
}
document.getElementById( "epimTokenInfo").innerHTML = retString;
}

};