Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Last active March 8, 2017 23:53
Show Gist options
  • Save gjyoung1974/682fe041ace6d1c84d3b5e6f8a7636dd to your computer and use it in GitHub Desktop.
Save gjyoung1974/682fe041ace6d1c84d3b5e6f8a7636dd to your computer and use it in GitHub Desktop.
Call a Lieberman ERPM API and return the auth token
/*
2016 Gordon Young, gjyoung1974@gmail.com
An example for calling the LiebSoft ERPM REST API
This Javascript calls the DoLogin2 method
and returns an API token for calling further methods
*/
var http = require('https'); //Call the API over HTTPS
//ignore Self-Signed SSL Certificate in test environment *** NEVER DO THIS IN PRODUCTION ***
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
/* Setup Values */
//API Properties
var Hostname = '13.85.10.4', Port = 443;
var DoLogin2Path = '/ERPMWebService/JSON/V2/AuthService.svc/DoLogin';
//ERPMWebService/AuthService.svc/REST
// username and password to collect API Token
var sUsername = 'app1', sPassword = 'Password1!'; //TODO source this from somewhere else
/* End Setup Values */
/* Main Application starts here */
//LDAP user vs static user:
//var auth_token_post_data = '{ "Authenticator":"ACME", "LoginType" : 2, "Password" : "n3wp@ss" , "Username" : "ACME\\\\testapp" }';
//static user configured in DB only
var auth_token_post_data = '{"LoginType": 1 ,"Password":"' + sPassword + '","Username":"' + sUsername + '"}';
var sAuthToken = f_Call_EPV_API(Hostname, Port,DoLogin2Path,auth_token_post_data);
/* Main Application ends here */
/* Utility functions follow below */
function f_Call_EPV_API(Hostname, Port, Path, post_data) {
var post_req = null;
var post_options = {
hostname: Hostname, port: Port, path: Path, method: 'POST',
headers: {
'User-Agent': 'node.js',
'Content-Type': 'application/json',
'accept': 'application/json',
'Cache-Control': 'no-cache',
'Content-Length': post_data.length
}
};
post_req = http.request(post_options, function (res) {
//console.log('HTTP STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (sAuthToken) {
console.log('Auth Token: ' + sAuthToken);
});
});
post_req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
post_req.write(post_data);
post_req.end();
return sAuthToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment