Forums

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

Having trouble with basic auth for HTTP request from ScriptRunner - Jira Server

Lachlan
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.
August 6, 2019

Hey everyone.

 

I am attempting to access the entity properties of an issue via a HTTP request. I can get an equivalent curl request to work, however when I attempt to execute this in ScriptRunner it fails.

Here is the script I am trying

import java.net.HttpURLConnection
import java.net.URL

def authStr = Base64.getEncoder().encodeToString("admin:admin".getBytes())

def baseURL = "http://localhost:2990/jira/rest/api/2/issue/JSD-11/properties/"
def url = new URL(baseURL);

def connection = url.openConnection() as HttpURLConnection;
connection.requestMethod = "GET"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Authenticate", "Basic " + authStr)

def headers = connection.headerFields

//connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect();




 

I am getting the following error message:

{"errorMessages":["You do not have permission to browse issues in this project."],"errors":{}}

This is what the logs say:

2019-08-07 13:57:09,347 WARN [httpclient.HttpMethodDirector]: Unable to respond to any of these challenges: {oauth=OAuth realm="http%3A%2F%2Flocalhost%3A2990%2Fjira"} 
2019-08-07 13:57:09,348 WARN [httpclient.HttpMethodBase]: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

 

As I said, I can get this work using a simple Curl  request, but I cannot seem to get the authorization working in ScriptRunner.

The curl request I use is: 

curl -u admin:admin http://localhost:2990/jira/rest/api/2/issue/JSD-11/properties/

I have tried other methods to make the HTTP call such as using org.apache.commons.httpclient.HttpClient but that yielded similar results.

The version of Jira I am using is pretty much a clean install - with little configuration changes made except for installing ScriptRunner.

1 answer

1 accepted

1 vote
Answer accepted
Tansu Akdeniz
Community Champion
August 7, 2019

Hi @Lachlan 

Response shows that there is an issue with authentication although you pass credentials.

Please try this one;

import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.HttpException
import org.apache.commons.httpclient.HttpStatus
import groovy.json.JsonSlurper
import com.atlassian.jira.config.properties.APKeys;
import org.apache.commons.httpclient.Credentials
import org.apache.commons.httpclient.UsernamePasswordCredentials
import org.apache.commons.httpclient.auth.AuthScope
import com.atlassian.jira.component.ComponentAccessor

String baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
String restURI = baseUrl + "/rest/api/2/issue/JSD-11/properties/";

HttpClient client = new HttpClient();
Credentials credentials = new UsernamePasswordCredentials("admin","admin");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, credentials);

GetMethod method = new GetMethod(restURI);

try {

int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("REST call failed: " + method.getStatusLine());
return;
}
JsonSlurper slurp = new JsonSlurper();
Reader reader = new InputStreamReader(method.getResponseBodyAsStream());
def result = slurp.parse(reader);
return result;

}catch (HttpException e) {
log.error("Exception: " + e.getMessage());
}

Regards

Lachlan
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.
August 7, 2019

@Tansu Akdeniz 

Amazing. It works perfectly. :)

Tansu Akdeniz
Community Champion
August 7, 2019

You are welcome @Lachlan :)

heysunny March 3, 2020

Hello @Tansu Akdeniz !

I've been trying to use your code in ScriptRunner to make a GET request from an outside API, however I can only get the response to show up when I'm returning it. If I want to assign the result to a variable (which I will need in order to split, trim, etc.), I get 'null' as a result. Do you perhaps know how to make this GET request so that I get a JSON object which then I can use to extract values from? (I'm very new to this.)

This is the response I want to extract the values (precisely the testCaseKeys) from:

image.png

This is my code. The first one works and gives me the response I want to work with and the second gives me null.

image.pngimage.png

Thanks so much!

Tansu Akdeniz
Community Champion
March 4, 2020

Hi @heysunny ,

Can you please try this one?

def parsedResult = result."items"
return parsedResult

Regards 

Suggest an answer

Log in or Sign up to answer