我正在尝试对我们的ALM 12.21服务器使用API,但总是以“401 Unauthorized”结束。似乎我正确地恢复了auth cookie,但是当我尝试做某事后,我就是未经授权的。
我用这个得到这个获得auth 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工作代码吗?
答案 0 :(得分:1)
您需要两个主要的Cookie才能使ALM REST API完美运行。
QCSession
almURL =&#34; https:// 。 .com / qcbin /"
authEndPoint = almURL +&#34; authentication-point / authenticate&#34;
qcSessionEndPoint = almURL +&#34; rest / site-session&#34;
成功回复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 它可能会给你一些想法。
答案 1 :(得分:1)
谢谢@Barney。你发送给我正确的方向:-)对于任何有兴趣的人,我都是这样管理的,例如:获取缺陷ID 473:
登录以创建CookieContainer,然后使用它来执行实际的ALM数据获取:
private void button1_Click(object sender, EventArgs e)
{
string almURL = @"https://url/qcbin/";
string domain = "domain";
string project = "project";
CookieContainer cookieContainer = LoginAlm2(almURL, "username", "password", domain, project);
HttpWebRequest myWebRequest1 = (HttpWebRequest)WebRequest.Create(almURL + "/rest/domains/" + domain + "/projects/" + project + "/defects/473");
myWebRequest1.CookieContainer = cookieContainer;
myWebRequest1.Accept = "application/json";
WebResponse webResponse1 = myWebRequest1.GetResponse();
StreamReader reader = new StreamReader(webResponse1.GetResponseStream());
string res = reader.ReadToEnd();
}
public CookieContainer LoginAlm2(string server, string user, string password, string domain, string project)
{
//Creating the WebRequest with the URL and encoded authentication
string StrServerLogin = server + "/api/authentication/sign-in";
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(StrServerLogin);
myWebRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Base64Encode(user + ":" + password);
WebResponse webResponse = myWebRequest.GetResponse();
CookieContainer c = new CookieContainer();
Uri uri = new Uri(server);
string StrCookie = webResponse.Headers.ToString();
string StrCookie1 = StrCookie.Substring(StrCookie.IndexOf("LWSSO_COOKIE_KEY=") + 17);
StrCookie1 = StrCookie1.Substring(0, StrCookie1.IndexOf(";"));
c.Add(new Cookie("LWSSO_COOKIE_KEY", StrCookie1) { Domain = uri.Host });
//Then the QCSession cookie
string StrCookie2 = StrCookie.Substring(StrCookie.IndexOf("QCSession=") + 10);
StrCookie2 = StrCookie2.Substring(0, StrCookie2.IndexOf(";"));
c.Add(new Cookie("QCSession", StrCookie2) { Domain = uri.Host });
//Then the ALM_USER cookie
string StrCookie3 = StrCookie.Substring(StrCookie.IndexOf("ALM_USER=") + 9);
StrCookie3 = StrCookie3.Substring(0, StrCookie3.IndexOf(";"));
c.Add(new Cookie("ALM_USER", StrCookie3) { Domain = uri.Host });
//And finally the XSRF-TOKEN cookie
string StrCookie4 = StrCookie.Substring(StrCookie.IndexOf("XSRF-TOKEN=") + 12);
StrCookie4 = StrCookie4.Substring(0, StrCookie4.IndexOf(";"));
c.Add(new Cookie("XSRF-TOKEN", StrCookie4) { Domain = uri.Host });
return c;
}
像魅力一样: - )