【问题标题】:Jira REST API - How to POST a new member to a project role?Jira REST API - 如何将新成员发布到项目角色?
【发布时间】:2014-04-26 04:24:57
【问题描述】:

我正在尝试使用 REST Api 来管理 JIRA 中的项目角色。我已经能够获取角色和“演员”的列表并删除角色成员。但我无法正确发布新角色成员。我不断收到 400 或 405 错误。我正在使用 HttpClient 4.3.2 和 Jira 6.0.2。这是我的代码:

// Set up ssl configuration as a user in JIRA instance

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://jira.install/rest/api/2/project/KEY/role/10000");

StringEntity input = new StringEntity("{\"group\":\"jira-users\" }");
input.setContentType("Application/json");
post.setEntity(input);

client.execute(post);

有没有人成功打过类似的电话?

【问题讨论】:

  • 我什至尝试了一个简单的 curl 请求,现在我收到一条错误消息,提示“无法反序列化 java.lang.String[] 的实例超出 VALUE_STRING tokent\n...”
  • 您的原始帖子在 StringEntity 周围存在语法错误。当您在浏览器中将成员添加到项目角色时,还值得使用 Chrome、检查元素并查看网络选项卡
  • 哦,谢谢,这只是我在提问时漏掉的一个错字。但我想我找到了答案,它需要用户名和密码才能发布到项目角色。我会尽快回复完整的答案。

标签: jira


【解决方案1】:

其余 api 需要对组角色的 POST 基本授权,但 API 文档中没有明确说明。所以这就是我如何让它工作的:

String auth = "username:password";
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US_ASCII")));
String authHeader = "Basic " + new String(encodedAuth);

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
StringEntity input;

try {
    input = new StringEntity("{\"group\":\"your-jira-group\"}");
    input.setContentType("Application/json");
    post.setEntity(input);
    post.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

    HttpResponse response = client.execute(post);
} catch (Exception ex) {
}

有两件事让我感到困惑...... Application/json 中的“A”需要大写,并且您需要授权会话。我选择了基本认证,用 curl 测试后使用了 HttpClient。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2014-12-05
    • 2015-10-10
    • 2020-12-07
    • 2021-06-12
    相关资源
    最近更新 更多