I'm not sure what I'm doing wrong: I'm trying to simply get a project list from our instance at https://<OurInstance>.atlassian.net via a REST call from within my Java application.
When I log into the instance via a browser and make the REST call:
https://<OurInstance>.atlassian.net/rest/api/2/project
I get the requested list in JSON format. But when I make the same call in Java from a plain J2SE application, I get a 401 response code - i.e. an 'Unauthorized'. As you can see in the code below, I do set the credentials. Also, I have used the same Java code successfully to make REST requests of another server (Sonarqube), but that one is 'http' rather than Jira's 'https' - do I need to do additional things for 'https'? Any help is much appreciated. Here's my code:
String urlString = "https://<OurInstance>.atlassian.net/rest/api/2/project";
String creds = user + ":" + password;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestProperty ("Authorization", "Basic " + Base64.getEncoder().encodeToString(creds.getBytes("UTF-8")));
connection.connect();
BufferedReader in = new BufferedReader (new InputStreamReader((InputStream) connection.getInputStream()));
...
} catch (Exception exc) {
System.err.println("Exception: " + exc);
}