New Ranks & Badges For The Community!
Notice something different? The ranks and associated badges have gone "Star Fleet". See what they all mean HERE
Highlighted
Cadet 1st Class
Cadet 1st Class
610 views

ALM API REST 12 erreur 401

Jump to solution

Hello, I have a code 401 
401 when i run request to retrieve the alm tests in java and i don't understand because i have thisn problem. 

Does anyone see why i have this problem.

My code is:

 

public class AppTest {

private static final String almURL = "https://.../qcbin";
private static final String isAuthenticatedPath = "authentication-point/authenticate";
private static final String AuthenticatedPath = "authentication-point/authenticate";
private static final String qcSiteSession = "rest/site-session";
private static final String logoutPath = "authentication-point/logout";
private static String lswoocookie;
private static String qcsessioncookie;


public static String strDomain = "dom";
public static String strProject = "proj";
public static String strUserName = "username";
public static String strPassword = "password";


public static Client client;
public static WebTarget target;
public static Invocation.Builder invocationBuilder;
public static Response res;
public static int Status = 0;
public static SSLContext sc;


private static String getEncodedAuthString() {
//String auth = strUserName + ":" + strPassword;
//byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
//String authHeader = "Basic " + new String(encodedAuth);

byte[] credBytes = (strUserName + ":" + strPassword).getBytes();
String authHeader = "Basic " + Base64.encodeBase64String(credBytes);

return authHeader;
}

public static void main(String args[]) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException{
/* SSL Context issue */
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}};

try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

/* create Rest client */
client = ClientBuilder.newBuilder().sslContext(sc).build();
/* Get LWSSO Cookie */
target = client.target(almURL).path(AuthenticatedPath);
System.out.println(target);
invocationBuilder = target.request(new String[] { "application/xml" });
invocationBuilder.header("Authorization", getEncodedAuthString());
res = invocationBuilder.get();
//ajout
lswoocookie = res.getCookies().get("LWSSO_COOKIE_KEY").getValue();
if(res!=null) {
System.out.println(res.getStatus());
System.out.println(res.getStatusInfo());

}

/* Check status */
if (res.getStatus() == 200) {
// get cookie from res
lswoocookie = res.getCookies().get("LWSSO_COOKIE_KEY").getValue();
/* Post QCSession with Cookie */
target = client.target(almURL).path(qcSiteSession);
System.out.println(target);
invocationBuilder = target.request();
invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
res = invocationBuilder.post(null);
qcsessioncookie = res.getCookies().get("QCSession").getValue();
if(res!=null) {
System.out.println(res.getStatus());
System.out.println(res.getStatusInfo());

}
// get cookie session
qcsessioncookie = res.getCookies().get("QCSession").getValue();

/* Get the first defect */
String midPoint = "rest/domains/" +strDomain+ "/projects/" +strProject;
target = client.target(almURL)
.path(midPoint).path("defects/1");
System.out.println(target);

invocationBuilder = target.request(new String[] { "application/xml"});
invocationBuilder.cookie("QCSession", qcsessioncookie);
invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
invocationBuilder.header("Authorization", getEncodedAuthString());
res = invocationBuilder.get();
if(res!=null) {
System.out.println(res.getStatus());
System.out.println(res.getStatusInfo());
}

/* Logout */
target = client.target(almURL).path(logoutPath);
invocationBuilder = target.request();
invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
invocationBuilder.cookie("QCSession", qcsessioncookie);
res = invocationBuilder.post(null);
if(res!=null) {
System.out.println(res.getStatus());
System.out.println(res.getStatusInfo());
}
}
else {
Status = 1;
}
}
}

console output:

JerseyWebTarget { https://.../authentication-point/authenticate }
200
OK
JerseyWebTarget { https://.../qcbin/rest/site-session }
201
Created
JerseyWebTarget { https://.../qcbin/rest/domains/DOM_C/projects/CAT_BAC_A_SABLE/defects/1 }
401
Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.
200
OK

 

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Commander
Commander

Hello fatima8313,

This error is due some browser configurations then is require to append that line at then end of the string. So in order to get it solved you can do it as below:

qcbin/authentication-point/authenticate?login-form-required=y 

Or

qcbin/rest/domains/DOMAIN/projects/PROJECT/defects/1?login-form-required=y'

I hope this will be helpful

Regards, 

 

 

View solution in original post

0 Likes
3 Replies
Highlighted
Commander
Commander

Hello fatima8313,

This error is due some browser configurations then is require to append that line at then end of the string. So in order to get it solved you can do it as below:

qcbin/authentication-point/authenticate?login-form-required=y 

Or

qcbin/rest/domains/DOMAIN/projects/PROJECT/defects/1?login-form-required=y'

I hope this will be helpful

Regards, 

 

 

View solution in original post

0 Likes
Highlighted
Cadet 1st Class
Cadet 1st Class

hello, I add this:

String midPoint = "rest/domains/" +strDomain+ "/projects/" +strProject;
target = client.target(almURL)
.path(midPoint).path("defects/1").queryParam("login-form-required";"y");

I have a code 200 but I do not recover the defects, the server responds 200

it does not work I feel that cookies are not recovered

thanks anyway
For your help

0 Likes
Highlighted
Cadet 1st Class
Cadet 1st Class

I found we must add other cookie and concatenated

almusercookie = res.getCookies().get("ALM_USER").getValue();
xsrftokencookie = res.getCookies().get("XSRF-TOKEN").getValue();

concatHeadCook = "QCSession=" + qcsessioncookie + ";" + "ALM_USER="
+ ";" + almusercookie + ";" + "XSRF-TOKEN=" + xsrftokencookie
+ ";" + "LWSSO_COOKIE_KEY=" + lswoocookie;

target = client.target(almURL).path(midPoint);

invocationBuilder = target.request(new String[] { "application/xml" })
.accept(new String[] { "application/xml" });

invocationBuilder.header("Cookie", concatHeadCook);
res = invocationBuilder.get();

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.