Authenticating with REST API
An authId is an authentication identifier passed back from iHub after successful authentication and is required for all subsequent REST API requests.
To generate the authId token, use a POST request for the /login resource with a username as a query parameter. Other parameters for /login are optional. An HTTP request does not encrypt the password field, so always use an HTTPS request for /login. For instructions to enable HTTPS support for REST API see Integrating Applications into BIRT iHub.
When successful, the REST API request returns an authentication identifier, authId with information about the user account. A REST API authentication identifier remains valid for 24 hours by default. The URI used to login is created in the /js/main.js file.
The index.html file starts the login function when the page loads using the following code in index.html:
<html lang="en" ng-app="flightApp" ng-controller="loginCtrl" ng-init="login()">
This starts the login function in /js/controller.js that uses a URI to the Actuate REST /login resource. The login function runs the following code:
$scope.login = function() {
var params= {
'username': mainAppCtrl.username,
'password': mainAppCtrl.password
};
API.Login.post(params, function(dataResponse) {
$scope.response = dataResponse;
A successful authentication returns a response body similar to the following:
{
"AuthId": "jIWy49iySIytHlaDNHSyStu6/KSKB2NJeGNo2RKryzupeK23GPyF9wBqBRH2+JDaKtThHpworWqsuMQTPZXR5Zam27DckdXTLxdDKfdJxxh6cYr75qMKUmyNJ+FKP4j3iEI2Zn04f61r0luc7tKFnZHoPPa9nTXxhrQ+lRiNs8t3NOcglCbGWHc+g64RaLQ0rflDawq/6FBfsh87w0D3Qs+raaJrTrbdiuJkrDXYq/GZthzB8lmRlhhlRi0MhTfSSDj2kwXqQpX5hepvtWVBDv24a+nhPjRYOQZOlRdSSvvhOLPgoNpZM0WmzdRj2+eqFTdDvj+lIcZtkO4Fh7KWaXOVRErKapokfIb/77X9h6de0YY63tm0OMzMkq/o5c6+",
"User": {
"Id": "200100000100",
"Name": "flightdemo",
"EmailAddress": "flightdemo@flightdemo.com",
"HomeFolder": "/Home/flightdemo"
}
}
Next, the login function sends a request to the /files resource to search for the file id of flight delay.data using the following code:
if (dataResponse.AuthId) {
$rootScope.userData = dataResponse;
$rootScope.AuthId = dataResponse.AuthId;
params = {
search :"/Resources/Data Objects/flight delay.DATA",
authId: $rootScope.AuthId
};
API.DownloadReport.query(params, function (dataResponse) {
A successful file search returns a response body similar to the following:
{
"ItemList": {
"File": [
{
"Id": "410300000100",
"Name": "flight delay.data",
"FileType": "DATA",
"PageCount": "0",
"Size": "10647543",
"Version": "1"
}
]
},
"TotalCount": "1"
}
Then the login function sends a request to the /dataobject resource with the file id of flight delay.data. This request searches for the geographic data set in the flight delay.data file using the following code:
if (dataResponse.TotalCount > 0) {
params = {
dataObjectId : dataResponse.ItemList.File[0].Id,
dataobjectElement : "geographic",
authId: $rootScope.AuthId
};
API.DownloadDataObjectElement.query(params, function (dataResponse) {
A successful data set search returns a response body similar to the following:
{
"data": [
{
"state": "AL",
"full_state": "Alabama",
"region": "South",
"scale": "5"
},
{
"state": "TN",
"full_state": "Tennessee",
"region": "South",
"scale": "5"
},
{
"state": "AR",
"full_state": "Arkansas",
"region": "South",
"scale": "4"
},
The login function finishes the login process by storing the state names in an array for the region associated with each state.
See the source code for the complete example.