[英]Authentication of HP ALM 12 REST API using Java
我正在使用 Java 來獲取測試用例信息。 我使用AuthenticateLoginLogoutExample
代碼作為參考。我能夠對用戶進行身份驗證,但是當我使用GET方法通過 REST API 檢索測試用例信息等簡單信息時,我收到了HTTP 401狀態代碼。
獲取 HTTP 401 狀態碼是正常的。 事實上,這意味着您可以 ping 服務器。 此外嘗試使用“http get”方法使用“{Host}/qcbin/rest/is-authenticated”捕獲響應。 您將在文檔的Rest Connector 類中找到 http get 方法。
如果您使用 AuthenticateLoginLogoutExample 作為參考,它應該沒問題。
您收到回復的可能原因:
HP 有自己的 base64encoder.java 類,您需要使用它。 你不能使用 java 中的標准。 您必須使用他們的版本。 在找到他們的編碼器類之前,我遇到了相同的 401 錯誤。
public class Base64Encoder {
private final static char[] ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static int[] toInt = new int[128];
static {
for (int i = 0; i < ALPHABET.length; i++) {
toInt[ALPHABET[i]] = i;
}
}
/**
* Translates the specified byte array into Base64 string.
*
* @param buf the byte array (not null)
* @return the translated Base64 string (not null)
*/
public static String encode(byte[] buf) {
int size = buf.length;
char[] ar = new char[((size + 2) / 3) * 4];
int a = 0;
int i = 0;
while (i < size) {
byte b0 = buf[i++];
byte b1 = (i < size) ? buf[i++] : 0;
byte b2 = (i < size) ? buf[i++] : 0;
int mask = 0x3F;
ar[a++] = ALPHABET[(b0 >> 2) & mask];
ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
ar[a++] = ALPHABET[b2 & mask];
}
switch (size % 3) {
case 1:
ar[--a] = '=';
case 2:
ar[--a] = '=';
}
return new String(ar);
}
}
盡管記錄正確,但不幸的是我無法從 microfocus 的說明中推斷出正在運行的代碼片段: https : //admhelp.microfocus.com/alm/en/12.60/api_refs/REST/Content/REST_API/Authenticate_LWSSO.html#alm_authenticate_Authentication
從 11 遷移到 12.60 后,我無法在 qc 中讀取或更新我的測試,我將分享它是如何為我工作的:
Map<String, String> map = new HashMap<String, String>();
map.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("$userX:$passwordY".getBytes() )) ;
map.put("Content-Type", "application/json");
String loginUrl = "http://qc/qcbin/api/authentication/sign-in" ;
response = null ;
try
{
response = con.httpGet( loginUrl , "" , map ) ;
}
catch (Exception e )
{
fail(e.getMessage() );
}
functions.method_log(String.valueOf( response.getStatusCode() + response.toString() ) ); //shoudl be empty, like the manual in a browser, an empty page is returned
loginUrl = "http://qc/qcbin/rest/domains/$DOMAIN/projects/$PROJECT/tests/?fields=id,name,user-04&query={user-04["+testUniqueIdentifier+"]}" ;
try
{
response = con.httpGet( loginUrl , "" , map ) ;
}
catch (Exception e )
{
log(e.getMessage() );
}
log(String.valueOf( response.getStatusCode() + response.toString() ) );
所以它是一個簡單的 2 個連續的 GET。 想最后加上signout,這里就不贅述了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.