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.