我尝试针对我们的 ALM 12.21 服务器使用 API,但总是以“401 Unauthorized”结束。看起来我正确地获取了身份验证 cookie,但是当我尝试执行此操作后,我就未经授权了。
我用这个 get this 来获取身份验证 cookie(似乎有效):
HttpWebRequest myauthrequest = (HttpWebRequest)WebRequest.Create("https://server/qcbin/authentication-point/alm-authenticate");
string AuthenticationXML = @"<alm-authentication>
<user>username</user>
<password>password</password>
</alm-authentication>";
byte[] Requestbytes = Encoding.UTF8.GetBytes(AuthenticationXML);
myauthrequest.Method = "POST";
myauthrequest.ContentType = "application/xml";
myauthrequest.ContentLength = Requestbytes.Length;
myauthrequest.Accept = "application/xml";
Stream RequestStr = myauthrequest.GetRequestStream();
RequestStr.Write(Requestbytes, 0, Requestbytes.Length);
RequestStr.Close();
HttpWebResponse myauthres = (HttpWebResponse)myauthrequest.GetResponse();
var AuthenticationCookie = myauthres.Headers.Get("Set-Cookie");
AuthenticationCookie = AuthenticationCookie.Replace(";Path=/;HTTPOnly", "");
我不确定是否需要.Replace。只是在某个地方读过它。不过,当尝试执行后续请求时,无论有没有它,我都会收到 401。
尝试例如获取 auth cookie 后:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://server/qcbin/rest/domains/FS/projects/P3602_SLS_Project/defects/1");
req.Method = "GET";
req.ContentType = "application/xml";
req.Accept = "application/octet-stream";
req.Headers.Set(HttpRequestHeader.Cookie, AuthenticationCookie);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream RStream2 = res.GetResponseStream();
XDocument doc = XDocument.Load(RStream2);
失败并返回 401。
有人拥有 ALM 12.21 REST API 的完整工作代码吗?
最佳答案
您需要两个主要 cookie 才能使 ALM REST API 完美运行。
- LWSSO_COOKIE_KEY
QC session
almURL = "https://..com/qcbin/"
authEndPoint = almURL +“身份验证点/身份验证”
qcSessionEndPoint = almURL +“休息/站点 session ”
获得 authEndPoint
的成功响应后,您将获得 LWSSO_COOKIE_KEY
在您对 qcSessionEndPoint
的下一个请求中使用该 cookie,它应该为您提供 QCSession cookie。
在后续请求中同时使用 LWSSO_COOKIE_KEY 和 QCSession cookie 以从 ALM 获取数据。
我发现您正在使用octet-stream
来获取缺陷响应。当我检查文档时,它可以返回以下类型之一。
"application/xml"
"application/atom+xml"
"application/json"
以防万一,如果您需要查看 python 中的一些工作实现,这里是 https://github.com/macroking/ALM-Integration/blob/master/ALM_Integration_Util.py 它可能会给你一些想法。
关于c# - HP ALM 12.21 REST API - 401 未经授权 - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43680666/