在JIRA门户中自动创建Bug,当使用Rest sured API(java)测试失败时,但输出为AuthenticationException:无效的用户名或密码,但在Postman中同样成功。我可以创建Bug并使用Jira的所有API,通过基本的用户名和密码身份验证本身,这些API工作正常。尝试了一些我在StackOverflow中发现的场景,但都有相同的响应(AuthenticationException:无效用户名或密码)
Scenario 1 :
------------
public void endTest(ITestResult result) throws JiraException {
System.out.println("Ended Test execution");
String username = "*******";
String password = "*******";
if (result.getStatus() == ITestResult.FAILURE) {
BasicCredentials cred = new BasicCredentials(username, password);
JiraClient jira = new JiraClient("http://localhost:8080/", cred);
Issue issueName = jira.createIssue("AAA", "Bug").field(Field.SUMMARY, result.getMethod().getMethodName() + "is failed due to: " + result.getThrowable().toString()).field(Field.DESCRIPTION, "Please take this Bug priority").execute();
System.out.println("Issue created in Jira with key : " + issueName.getKey());
}
}
Response : net.rcarz.jiraclient.JiraException: Failed to retrieve issue metadata
401 Unauthorized: {"servlet":"Stapler","message":"Invalid password/token for user: *******","url":"/rest/api/latest/issue/createmeta","status":"401"}
Scenario 2 :
------------
public static String invokePostMethod() throws ClientHandlerException, AuthenticationException, IOException {
String username = "*******";
String password = "*******";
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/rest/api/2/issue");
String data = "{\n" +
" \"fields\": {\n" +
" \"project\": {\n" +
" \"key\":\"QQQ\"\n" +
" },\n" +
" \"summary\": \"creating Bug QQQ project\",\n" +
" \"description\": \"Exception three\",\n" +
" \"issuetype\": {\n" +
" \"name\":\"Bug\"\n" +
" }\n" +
" }\n" +
"}";
String auth = new String(Base64.encode(username + ":" + password));
ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").post(ClientResponse.class, data);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
} else if (statusCode == 403) {
throw new AuthenticationException("Forbidden");
} else if (statusCode == 200 || statusCode == 201) {
System.out.println("Ticket Create succesfully");
} else {
System.out.print("Http Error : " + statusCode);
}
// ******************************Getting Responce body*********************************************
BufferedReader inputStream = new BufferedReader(new InputStreamReader(response.getEntityInputStream()));
String line = null;
while ((line = inputStream.readLine()) != null) {
System.out.println(line);
}
return response.getEntity(String.class);
}
Response: Exception caught - Invalid Username or Password
找到解决方案:我的错,我愚蠢地检查以下条件看起来像两个服务器在同一端口中运行 检查端口中运行多少服务的命令:sudo lsof -i:8080 命令杀死端口:kill -9