实际上,我的任务是从 Excel 工作表中发布 JIRA 中的错误。
现在我可以在 Bugzilla 中发布错误。请任何人帮助我编写有关如何使用 REST API 登录和发布错误的 Java 代码。我有一个 Perl 语言的示例,我想要 Java 语言的示例。
我尝试使用以下代码,我可以登录,但现在我想使用 JIRA REST API 执行所有任务。
HttpPost httpost = new HttpPost("https://id.atlassian.com/login?continue=https://jira.atlassian.com/secure/Dashboard.jspa&application=jac");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "sxxxxxxx@live.com"));
nvps.add(new BasicNameValuePair("password", "xxxxxxxxxn"));
nvps.add(new BasicNameValuePair("csrfToken", xcrfTokenVal));
nvps.add(new BasicNameValuePair("continue", "https://jira.atlassian.com/secure/Dashboard.jsp"));
nvps.add(new BasicNameValuePair("application", "jac"));
httpost.setEntity(new UrlEncodedFormEntity(nvps));
response = httpclient.execute(httpost);
System.out.println("Response " + response.toString());
entity = response.getEntity();
//System.out.println("Double check we've got right page " + EntityUtils.toString(entity));
System.out.println("Response from : " + response.getStatusLine());
最佳答案
您必须对“http://JIRA_URL/rest/auth/1/session ”执行 POST,并使用 JSessionID 设置 Cookie 才能执行其他请求。在 Jersey ,基本上你会这样做:
jsonParameters = "{\"username\":\"usernameValue\", \"password\": \"passwordValue\"}";
WebResource webResource = client.resource("http://<JIRA_URL>/rest/auth/1/session");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, jsonParameters);
在响应中,您将获得 JSessionID,因此您必须将其保存在 Cookie 中并使用它。在 Jersey :
import javax.ws.rs.core.Cookie;
...
Cookie cookie = new Cookie(jsession.getName(), jsession.getValue());
response = webResource.type(MediaType.APPLICATION_JSON).cookie(cookie)
.post(ClientResponse.class, jsonParameters);
JIRA Rest 文档:
关于java - 如何使用 REST API 和 POST 问题登录 JIRA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183702/