I’m having trouble authenticating with the Jira API in my Spring Boot project. The same request works fine in Postman, but I get a 401 error in my code.
Here’s my authentication setup:
public class ApiConnector {
private String baseUrl = "mycompany.atlassian.net";
private RestTemplate client;
private HttpHeaders headers;
private HttpStatus responseStatus;
public ApiConnector() {
this.client = new RestTemplate();
this.headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Accept", "*/*");
headers.add("Connection", "keep-alive");
String encodedAuth = Base64.getEncoder().encodeToString("myuser:mypass".getBytes());
headers.add("Authorization", "Basic " + encodedAuth);
}
public String fetchData(String endpoint) {
HttpEntity<String> request = new HttpEntity<>("", headers);
ResponseEntity<String> response = client.exchange(baseUrl + endpoint, HttpMethod.GET, request, String.class);
this.responseStatus = response.getStatusCode();
return response.getBody();
}
}
I’ve tried hardcoding the Base64 string, but it still doesn’t work. Any ideas on what might be causing this authentication failure?
I encountered a similar issue when integrating with Jira’s API in a Spring Boot project. The problem might be related to how you’re constructing the base URL. Try modifying your baseUrl to include the protocol and API version:
private String baseUrl = "https://mycompany.atlassian.net/rest/api/2/";
Also, ensure your API token is correct. Jira now prefers API tokens over passwords for authentication. You can generate one in your Atlassian account settings.
If that doesn’t resolve the issue, enable logging for your RestTemplate to see the exact request being sent. You might spot differences between your code and Postman requests. Lastly, double-check your permissions in Jira to ensure your account has the necessary access rights for the API endpoints you’re calling.
Having dealt with Jira API authentication in Spring Boot before, I can say it can be tricky. One thing that stands out is your baseUrl. It’s missing the protocol and API path. Try changing it to:
private String baseUrl = "https://mycompany.atlassian.net/rest/api/2/";
Also, Jira now prefers API tokens over passwords. Generate one from your Atlassian account and use that instead of your password in the authentication string.
If you’re still having issues, enable debugging for RestTemplate. Add this to your configuration:
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000);
this.client = new RestTemplate(factory);
This will give you more detailed error information. Lastly, verify your network isn’t blocking the request. Sometimes corporate firewalls can interfere with API calls.
hey, had similar issue. make sure u’re using https in baseUrl like this:
https://mycompany.atlassian.net/rest/api/2/
also, jira prefers API tokens now. generate one in ur atlassian account settings & use that instead of password. if still no luck, check ur firewall settings. some corporate networks block these requests.