I am using Java to get the test case information. I am using the AuthenticateLoginLogoutExample code as reference.I was able to authenticate the user but I receive the HTTP 401 status code when I use GET method to retrieve simple information like testcase information etc through REST API.
Authentication of HP ALM 12 REST API using Java
2.1k Views Asked by user1955851 AtThere are 4 best solutions below
On
If you are using the AuthenticateLoginLogoutExample as reference it should be OK.
Possible causes of response you recieved:
- The user is not assigned to a project you're interested in;
- You have performed a logout operation;
- Not ideal cookie management. The ALM requires the session keeping on REST interoperation. It is implemented with LWSSO_COOKIE_KEY coolie in request. It is sended with response from server as resul of successful session opening. If you loose it you will be faced with such response code.
On
HP has their own base64encoder.java class that you need to use. You can't use the one that's standard in java. You must use their version. I was getting the same 401 error until I found their encoder class.
public class Base64Encoder {
private final static char[] ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static int[] toInt = new int[128];
static {
for (int i = 0; i < ALPHABET.length; i++) {
toInt[ALPHABET[i]] = i;
}
}
/**
* Translates the specified byte array into Base64 string.
*
* @param buf the byte array (not null)
* @return the translated Base64 string (not null)
*/
public static String encode(byte[] buf) {
int size = buf.length;
char[] ar = new char[((size + 2) / 3) * 4];
int a = 0;
int i = 0;
while (i < size) {
byte b0 = buf[i++];
byte b1 = (i < size) ? buf[i++] : 0;
byte b2 = (i < size) ? buf[i++] : 0;
int mask = 0x3F;
ar[a++] = ALPHABET[(b0 >> 2) & mask];
ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
ar[a++] = ALPHABET[b2 & mask];
}
switch (size % 3) {
case 1:
ar[--a] = '=';
case 2:
ar[--a] = '=';
}
return new String(ar);
}
}
On
Although correctly documented, unfortunately I was not able to deduce a running snippet of code from microfocus's instructions: https://admhelp.microfocus.com/alm/en/12.60/api_refs/REST/Content/REST_API/Authenticate_LWSSO.html#alm_authenticate_Authentication
I was not able to have my tests read or updated in qc following a migration from 11 to 12.60 and I will share how it worked for me:
Map<String, String> map = new HashMap<String, String>();
map.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("$userX:$passwordY".getBytes() )) ;
map.put("Content-Type", "application/json");
String loginUrl = "http://qc/qcbin/api/authentication/sign-in" ;
response = null ;
try
{
response = con.httpGet( loginUrl , "" , map ) ;
}
catch (Exception e )
{
fail(e.getMessage() );
}
functions.method_log(String.valueOf( response.getStatusCode() + response.toString() ) ); //shoudl be empty, like the manual in a browser, an empty page is returned
loginUrl = "http://qc/qcbin/rest/domains/$DOMAIN/projects/$PROJECT/tests/?fields=id,name,user-04&query={user-04["+testUniqueIdentifier+"]}" ;
try
{
response = con.httpGet( loginUrl , "" , map ) ;
}
catch (Exception e )
{
log(e.getMessage() );
}
log(String.valueOf( response.getStatusCode() + response.toString() ) );
So it is a simple 2 successive GETs. Think to add the signout in the end, which I am not describing here.
Getting HTTP 401 Status code is normal. In Fact that meas you are able to ping the server. Furthermore try to Capture the response using "{Host}/qcbin/rest/is-authenticated" using "http get" method. You will find http get methond in Rest Connector class in Documentation.