ALM REST API v12.50错误401

时间:2016-02-15 23:09:50

标签: c# api rest alm

下午好, 我正在开发一个需要使用REST API连接到HPQC v12.50的C#应用​​程序。我已经能够进行身份验证,然后我尝试重新使用我收到的cookie作为webresponse来执行查询。但是,我遇到错误401,在阅读了多个论坛和惠普关于REST API的文档之后,我无法弄清楚我做错了什么。

这是我的一大堆代码:

    public void LoginAlm(string StrServer, string StrUser, string StrPassword, string StrDomain, string StrProject)
    {
        //First, I am authenticating to the server (this works fine)
        string StrServerLogin = StrServer + "/authentication-point/authenticate";
        HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(StrServerLogin);
        myWebRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Base64Encode(StrUser + ":" + StrPassword);
        WebResponse webResponse = myWebRequest.GetResponse();

        //Then, I am extracting the cookie from the header to re-use it for me subsequent requests.
        HttpWebRequest myWebRequest1 = (HttpWebRequest)WebRequest.Create(StrServer + "/rest/domains/"+StrDomain+"/projects/"+StrProject+"/defects&#1");
        string StrCookie = webResponse.Headers.ToString();
        StrCookie = StrCookie.Substring(StrCookie.IndexOf("Set-Cookie:") + 12);
        StrCookie = StrCookie.Substring(0, StrCookie.IndexOf("\r\n"));
        myWebRequest1.Headers[HttpRequestHeader.Cookie] = StrCookie;

        //Then, I am trying to perform my request, and the following line would always return an error 401.
        WebResponse webResponse1 = myWebRequest1.GetResponse();

        StreamReader reader = new StreamReader(webResponse1.GetResponseStream());
        textBox6.Text = reader.ReadToEnd();
    }

我在HP的文档中注意到的一件事是我应该收到一个令牌作为回复,但它似乎并不存在......

这是我第一次使用REST API,如果它看起来微不足道,我很抱歉,但我已经浪费了太多时间在这上面了!

3 个答案:

答案 0 :(得分:1)

最后,我重新检查了REST API的文档,发现要使用的正确身份验证URL是" / api / authentication / sign-in" ALM v12.50。

它将在webresponse的标题中返回以下cookie: - LWSSO_COOKIE_KEY - QCSession - ALM_USER - XSRF-TOKEN

这些Cookie必须添加到Cookie容器中的下一个webrequest中。对于那些需要代码的人来说,这里是C#:

    public void LoginAlm(string StrServer, string StrUser, string StrPassword, string StrDomain, string StrProject)
    {
        //Creating the WebRequest with the URL and encoded authentication
        string StrServerLogin = StrServer + "/api/authentication/sign-in";
        HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(StrServerLogin);
        myWebRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Base64Encode(StrUser + ":" + StrPassword);
        WebResponse webResponse = myWebRequest.GetResponse();

        //Once this is done, we create a new request, this example to get the information about defect #1 on a given project
        HttpWebRequest myWebRequest1 = (HttpWebRequest)WebRequest.Create(StrServer + "/rest/domains/"+StrDomain+"/projects/"+StrProject+"/defects/1");

        //Then, we create a cookie container to the webrequest where we will store the cookie that we've received from the authentication webresponse
        myWebRequest1.CookieContainer = new CookieContainer();
        Uri target = new Uri(StrServer);

        //We extract the LWSSO_COOKIE_KEY cookie and add it to the cookie container
        string StrCookie = webResponse.Headers.ToString();
        string StrCookie1 = StrCookie.Substring(StrCookie.IndexOf("LWSSO_COOKIE_KEY=") + 17);
        StrCookie1 = StrCookie1.Substring(0, StrCookie1.IndexOf(";"));
        myWebRequest1.CookieContainer.Add(new Cookie("LWSSO_COOKIE_KEY", StrCookie1) { Domain = target.Host });

        //Then the QCSession cookie
        string StrCookie2 = StrCookie.Substring(StrCookie.IndexOf("QCSession=") + 10);
        StrCookie2 = StrCookie2.Substring(0, StrCookie2.IndexOf(";"));
        myWebRequest1.CookieContainer.Add(new Cookie("QCSession", StrCookie2) { Domain = target.Host });

        //Then the ALM_USER cookie
        string StrCookie3 = StrCookie.Substring(StrCookie.IndexOf("ALM_USER=") + 9);
        StrCookie3 = StrCookie3.Substring(0, StrCookie3.IndexOf(";"));
        myWebRequest1.CookieContainer.Add(new Cookie("ALM_USER", StrCookie3) { Domain = target.Host });

        //And finally the XSRF-TOKEN cookie
        string StrCookie4 = StrCookie.Substring(StrCookie.IndexOf("XSRF-TOKEN=") + 12);
        StrCookie4 = StrCookie4.Substring(0, StrCookie4.IndexOf(";"));
        myWebRequest1.CookieContainer.Add(new Cookie("XSRF-TOKEN", StrCookie4) { Domain = target.Host });

        //We then send our webrequest and collect the webresponse
        WebResponse webResponse1 = myWebRequest1.GetResponse();
        StreamReader reader = new StreamReader(webResponse1.GetResponseStream());
    }

我希望它会有所帮助!

答案 1 :(得分:0)

HP版本12在身份验证方面略有改变。 他们已将网址从身份验证点/身份验证更改为"/authentication-point/alm-authenticate"

成功发布上述网址后,用户需要发布"/rest/site-session"网址以及以下请求正文。

"<alm-authentication><user><![CDATA[username]]></user><password><![CDATA[password]]></password></alm-authentication>"

答案 2 :(得分:0)

authentication-point/authenticated发帖后请求  你必须向rest/site-session发出另一个请求请求,然后只创建一个会话  能够做进一步的操作