Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

JIRA REST API authentication issue

Yotam Sagiv June 23, 2014

Hi, so I'm trying to create a Java program that makes use of the JIRA REST API in order to receive information about the amount of issues in a certain project. Using curl, this is easy. Using Java, I run into a 401 Unauthorized error, and I'm not entirely sure why. Can someone help? My code is below.

import java.io.*;
import java.net.*;
import org.apache.commons.codec.binary.Base64;

public class JiraConnector
{
	public static void main(String[] args)
	{
		HttpURLConnection connection = null;
		try {
			//store necessary query information
			URL jiraURL = new URL("http://jira.somecompany.com/rest/api/2/search");
			String data = "{\"jql\":\"project = PROJ\"}";
			byte[] dataBytes = data.getBytes("UTF-8");
			String login = "user:password";
			byte[] encodedBytes = Base64.encodeBase64(login.getBytes());
			
			//establish connection and request properties
			connection = (HttpURLConnection) jiraURL.openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Accept", "*/*");
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Authorization", "Basic " + encodedBytes);
			connection.setUseCaches(false);
			connection.setDoOutput(true);
			connection.setDoInput(true);

			connection.connect();
			
			DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
				wr.write(dataBytes);
				wr.flush();
				wr.close();
			
			Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			for (int c; (c = in.read()) >= 0; System.out.print((char)c));
		
		} catch (MalformedURLException e) {
			System.err.println("MalformedURLException: " + e.getMessage());
		} catch (IOException e) {
			System.err.println("IOException: " + e.getMessage());
			
			if (connection != null) {
				try {
					System.out.println(connection.getResponseMessage());
					
					InputStream errorStream = connection.getErrorStream();
					if (errorStream != null) {
						Reader in = new BufferedReader(new InputStreamReader(errorStream));
						for (int c; (c = in.read()) >= 0; System.out.print((char) c));
					}
				} catch(IOException e2) {}	
			}
		}		
	}
}

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Yotam Sagiv June 30, 2014

Update:

Found the problem. Basic authorization needed me to send a String of the encoded bytes, rather than a byte[].

Marketa Dvorackova March 5, 2015

Thank you so much as much as it's possible. You've just saved me hours of wondering what the hell is going on with my script. Really thanks!

0 votes
Tiago Comasseto
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.
June 23, 2014

Hi Yotam,

We how some samples of OAuth providers in a number of languages, including JAVA. You may visit the sample repo on Bitbucket to download and work with these samples. You may also check this documentation for more details.

Cheers

TAGS
AUG Leaders

Atlassian Community Events