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);
}
Answering my own question: it appears that password based basic authentication is not (no longer?) supported. When I replaced the password in the above code with an API Token, everything worked. Since there is so much contradictory and/or obsolete information on the Atlassian site regarding this, this is the basic authentication guide that got me to the right answer.
Follow-up to my own question: after posting here, I read some posts on the Atlassian web site that suggest that the basic authentication I'm trying to do has been deprecated? That same post stated that OAuth was now the only way to make REST calls? What's baffling is that on other Atlassian web pages, basic authentication is still described in detail and as one of three ways (the third being cookies) to make REST calls.
What's more, the pages that guide you through OAuth based authentication aren't even right - the Settings > Applications > Application Links page it refers to doesn't exist - at least not in my version of Jira (hosted at atlassian.net:
https://developer.atlassian.com/server/jira/platform/oauth/
I guess they are outdated? All I'm looking for is the simplest way to make a REST call from Java on a Jira instance that uses https. I don't really care if it's Basic OAuth, or cookies - but I'd love to see a complete example or a link to Guide pages that are actually accurate.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I also found this Atlassian developer guide on how to do OAuth in Java with, supposedly, example code/jar that's downloadable:
https://developer.atlassian.com/server/jira/platform/jira-rest-api-example-oauth-authentication-6291692/
But clicking on either jar link (source or binary) gives me a 404 Error - i.e. the files aren't found. So much for Atlassian keeping its documentation up-to-date :-( So frustrating!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.