简体   繁体   中英

HP ALM Rest API 401 Authentication error?

I am using HP ALM Version 12.55.113. I am trying to upload test results automatically via REST-API. After authenticating and trying to read a simple defect, I am receiving a 401 not authenticated error. I am retrieving a valid LWSSO and QCSession Cookie, using the following example code:

public class App {

    private static final String almURL = "http://something.com/qcbin";
    private static final String isAuthenticatedPath = "authentication-point/authenticate";
    private static final String qcSiteSession = "rest/site-session";
    private static final String authTest = "rest/is-authenticated";
    private static final String logoutPath = "authentication-point/logout";
    private static String lswoocookie;
    private static String qcsessioncookie;

    public static String strDomain = "domain";
    public static String strProject = "project";
    public static String strUserName = "user";
    public static String strPassword = "pass";

    public static Client client;
    public static WebTarget target;
    public static Invocation.Builder invocationBuilder;
    public static Response res;

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

        return authHeader;
    }

    private static void loggedIn() {
        WebTarget targeta = client.target(almURL).path(authTest);
        invocationBuilder = targeta.request();
        invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
        Response resa = invocationBuilder.get();
        System.out.println("Logged in: " + resa);
    }

    public static void main(String args[]) {
        client = ClientBuilder.newBuilder().build();
        loggedIn();
        /* Get LWSSO Cookie */
        target = client.target(almURL).path(isAuthenticatedPath);
        invocationBuilder = target.request(new String[] { "application/xml" });
        invocationBuilder.header("Authorization", getEncodedAuthString());
        res = invocationBuilder.get();
        System.out.println(res);
        lswoocookie = res.getCookies().get("LWSSO_COOKIE_KEY").getValue();
        System.out.println("LSWOO: " + lswoocookie);

        /* Get QCSession Cookie */
        target = client.target(almURL).path(qcSiteSession);
        invocationBuilder = target.request(new String[] { "application/json" });
        invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
        res = invocationBuilder.post(null);
        qcsessioncookie = res.getCookies().get("QCSession").getValue();
        System.out.println("QCSession: " + qcsessioncookie);
        System.out.println(target);

        /* Get the first defect */
        String midPoint = "rest/domains/" + strDomain + "/projects/" + strProject;
        target = client.target(almURL).path(midPoint).path("defects/1");
        invocationBuilder = target.request(new String[] { "application/json" });
        invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
        invocationBuilder.cookie("QCSession", qcsessioncookie);
        res = invocationBuilder.get();
        System.out.println(res);
        loggedIn();

        /* Logout */
        target = client.target(almURL).path(logoutPath);
        invocationBuilder = target.request();
        invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
        invocationBuilder.cookie("QCSession", qcsessioncookie);
        res = invocationBuilder.post(null);
        System.out.println(res);
    }

I am getting the following console ouput:

Logged in: InboundJaxrsResponse{context=ClientResponse{method=GET, uri= http://bla.bla:8080/qcbin/rest/is-authenticated , status=401, reason=Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.}}

InboundJaxrsResponse{context=ClientResponse{method=GET, uri= http://bla.bla/qcbin/authentication-point/authenticate , status=200, reason=OK}}

LSWOO: IvG6JIOKRTAigQV6...and so on

QCSession: MTEzNTU...and so on

JerseyWebTarget { http://bla.bla:8080/qcbin/rest/site-session }

InboundJaxrsResponse{context=ClientResponse{method=GET, uri= http://bla.bla/qcbin/rest/domains/WARTUNG/projects/Testautomatisierung/defects/1 , status=401, reason=Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.}}

Logged in: InboundJaxrsResponse{context=ClientResponse{method=GET, uri= http://bla.bla/qcbin/rest/is-authenticated , status=200, reason=OK}}

InboundJaxrsResponse{context=ClientResponse{method=POST, uri= http://sth.com/qcbin/authentication-point/logout , status=200, reason=OK}}

Has anyone any idea what I did wrong? Tried so many ways but it never worked. Thanks in advance :)

我认为您还需要在请求中传递 XSRF-TOKEN(可能还有 ALM_USER 和 JSESSIONID)才能获得缺陷

I guess the problem is due to the content type for QCSession end-point.

public class UpdateALM {    
    private static final String qcbinURI = "https://alm_url/qcbin";
    private static final String isAuthenticatedPath = "rest/is-authenticated";
    private static final String logoutPath = "authentication-point/logout";
    private static final String domainsPath = "rest/domains";
    private static Cookie cookie;

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

        return authHeader;
    }

    public boolean isAuthenticated() {
        target = client.target(qcbinURI).path(isAuthenticatedPath);
        invocationBuilder = target.request(new String[] { "application/xml" });
        invocationBuilder.cookie(cookie);
        Response res = invocationBuilder.get();
        return res.getStatus() == 200;
    }

    public void updateALMStatus() throws Exception {
        try {           
            target = client.target(
                    "https://alm_url/qcbin").path(
                    "authentication-point/authenticate");
            invocationBuilder = target.request("text", "plain");
            invocationBuilder.header("Authorization", getEncodedAuthString());

            res = invocationBuilder.get();
            cookie = ((Cookie) res.getCookies().get("LWSSO_COOKIE_KEY"));

            getTestSet(test_set);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

Refer: https://admhelp.microfocus.com/alm/en/12.60/api_refs/REST_TECH_PREVIEW/ALM_REST_API_TP.html#REST_API_Tech_Preview/General/Authenticate_LWSSO.html%3FTocPath%3DHow%2520to%2520Do%2520Common%2520Tasks%7CAuthenticate%7C_____1

I also came across the same issue as Bastian and i was able to resolve it.

The issue was the way in which the cookies was passed along the request.

Instead of this :

    invocationBuilder.cookie("LWSSO_COOKIE_KEY", lswoocookie);
    invocationBuilder.cookie("QCSession", qcsessioncookie);
    res = invocationBuilder.get();

Pass the cookies in the header as a concatenated String as shown below:

String concatenatedHeaderCookieString = "QCSession=" + qcsessioncookie + ";" + "ALM_USER=" + ";" + almuser + ";" + "XSRF-TOKEN=" + xsrftoken + ";"+ "LWSSO_COOKIE_KEY=" + lswoocookie;

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

Result : InboundJaxrsResponse{context=ClientResponse{method=GET, uri=https://xxxxx.xx.xxxxx.com/qcbin/rest/domains/RELEASES/projects/2019/defects/1, status=200, reason=OK}}

And this would work like a charm!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM