Hello!
I've been trying to get my Java code working with the Jira server rest api v2.
I currently have this code:
private static final String JiraBase = "https://mycompanyurl.com"
private static final String JiraEmail = "myemail@mycompanyurl.com"
private static final String PAT = "ljknfg9u8h5h9nner9gbner098honeg+on"
try {
String apiURL = JiraBase + "/rest/api/2/myself";
rustManager[] trustAllCertificates = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCertificates, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String auth = JiraEmail + ":" + PAT;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", authHeader);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.setSSLSocketFactory(sslSocketFactory);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP - oK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()))
);
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in .readLine()) != null) {
response.append(inputLine);
}
in .close();
System.out.printLn("Response: " + response.toString());
} else {
System.out.printLn("GET request failed");
}
Does anyone see anything I am missing?
It's just odd that I can access /rest/api/2/myself from the web browser but not in the Java app. The response code I get is 403 in the java app.
This is rather over complicating it but since this is tagged as server, I suspect you're not setting the right auth header?
If that is a "real" PAT, then you'd only do "Bearer <PAT>". If it's basic username:password, then it would be "Basic <base64 user:pass>". So just to make sure because the snippet says PAT but you're using it as a password.
Hi @stealthrt
I don't see any specific error with your code, but I would suggest you to use a tool like Postman to test the API endpoint and authentication. This can help you isolate the issue and determine if it's a problem with the Java code or the Jira instance.
Additionally, we have a developer community and you can check there for better guidance:
https://community.developer.atlassian.com/
Regards,
Dilip
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!
Help us plant more trees
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.