I’m able to authenticate with the Jira API through Postman without any issues. However, when I try to implement this authentication in my Spring Boot application, I repeatedly encounter a 401 unauthorized error. Here’s the relevant code I’ve been working with:
public class ApiClient {
private String baseUrl = "mycompany.atlassian.net";
private RestTemplate restTemplate;
private HttpHeaders httpHeaders;
private HttpStatusCode responseStatus;
public ApiClient() {
this.restTemplate = new RestTemplate();
this.httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json");
httpHeaders.add("Accept", "*/*");
httpHeaders.add("Connection", "keep-alive");
String authString = "Basic " + new String(Base64.getEncoder().encode("myuser:mytoken".getBytes()));
System.out.println(authString);
httpHeaders.add("Authorization", authString);
}
public String makeGetRequest(String endpoint) {
HttpEntity<String> entity = new HttpEntity<String>("", httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(baseUrl + endpoint, HttpMethod.GET, entity, String.class);
this.setResponseStatus(response.getStatusCode());
return response.getBody();
}
public HttpStatusCode getResponseStatus() {
return responseStatus;
}
public void setResponseStatus(HttpStatusCode status) {
this.responseStatus = status;
}
public String fetchIssueStatus(String issueKey) throws JSONException {
ApiClient client = new ApiClient();
String result = client.makeGetRequest("/rest/api/2/issue/" + issueKey + "?fields=status");
JSONObject json = new JSONObject(result);
JSONObject fields = (JSONObject) json.get("fields");
JSONObject statusObj = (JSONObject) fields.get("status");
String statusName = statusObj.getString("name");
return statusName;
}
}
I even tried hardcoding the authorization header directly, but still faced the same issue. The credentials I use work perfectly when tested in Postman, so I’m puzzled about what’s going wrong in my Spring Boot implementation.