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 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. A REST API authentication identifier remains valid for 24 hours by default.
This example uses Objective-C code to make an authentication request from the iHub server. After collecting the username and password from the application's user interface, these values are stored in an NSData object, with the following code:
-(void) login {
NSString *yourName = self.username.text;
NSString *password = self.password.text;
NSString *post =[NSString stringWithFormat:
@"username=%@&password=%@",yourName,password ];
NSData *postData =
[post dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",
(unsigned long)[postData length]];
Then the URI to authenticate a user with the REST API is built. An NSMutableURLRequest uses the URI and attaches the authentication values as the body of the HTTP POST request, with the following code:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString
stringWithFormat:@"%@%@",
REST_API_URL,
@"login"]]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
After checking for errors, the JSON formatted response from the iHub server is then converted into a Foundation object using the NSJSONSerialization class. This Foundation object is converted into an NSDictionary for use in other REST API requests, as shown in the following code:
NSDictionary *loginResponse = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableLeaves error:nil];
_authId = [loginResponse objectForKey:@"AuthId"];
See the source code for the complete example.
The source code to send RESTful requests using the Android SDK depends on the HTTP library that you are using. For example, if you are using Java’s HttpURLConnection class, your RESTful request to the login endpoint can be similar to the following code:
JSONObject authIDResponse = WebServiceUtil.requestWebService(
"http://ihubserver:5000/ihub/v1/login");
 
public static JSONObject requestWebService(String targetURL) {
disableConnectionReuseIfNecessary();
 
URL restURL = new URL(targetURL);
 
String formValues =
"user=" + URLEncoder.encode("demo", "UTF-8") +
"&password=" + URLEncoder.encode("demo", "UTF-8");
 
HttpURLConnection restConnection = (HttpURLConnection) restURL.openConnection();
restConnection.setRequestMethod("POST");
restConnection.setRequestProperty("Accept", "application/json");
restConnection.setFixedLengthStreamingMode(formValues.getBytes().length);
restConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 
restConnection.setConnectTimeout(15000);
restConnection.setReadTimeout(10000);
restConnection.setDoOutput(true);
 
//PrintWriter formStream = new PrintWriter(restConnection.getOutputStream());
//formStream.print(formValues);
//formStream.close();
 
DataOutputStream formStream = new DataOutputStream (
restConnection.getOutputStream ());
formStream.writeBytes (formValues);
formStream.flush ();
formStream.close ();
 
if (restConnection.getResponseCode() != 200) {
throw new RuntimeException("Connection Error code : " + restConnection.getResponseCode());
}
 
InputStream restResponse = new BufferedInputStream(
restConnection.getInputStream());
 
responseText = EntityUtils.toString(httpresponse.getEntity());
 
return new JSONObject(getResponseText(restResponse));
 
restConnection.disconnect();
}
private static String getResponseText(InputStream inStream) {
// http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
return new Scanner(inStream).useDelimiter("\\A").next();
}