Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Logging into REST api via Java

stealthrt May 28, 2025 edited

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.

2 answers

1 accepted

0 votes
Answer accepted
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2025

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.

stealthrt May 29, 2025 edited

Thank you! It was because of having basic instead of bearer!

0 votes
Dilip
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 29, 2025

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

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

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
AUG Leaders

Atlassian Community Events