It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

JIRA Java Rest client Api returns 401 on authentication.

While attempting to connect with JIRA and get issues I get Error 401 for one of the account (Jira Cloud). This same configuration works well with other account (Jira Server). 

When navigated to "View OAuth Access Tokens" section i see this. Not sure if this is the issue...but for the other account I see one created for me under same section for which the authentication works well and was able to get the data's as required.

Attached screenshot for reference. Please guide me with this.

Capture.PNG

download.png

 

The sample code is below:

 public JiraClient() {
// this is constructor
this.username = properties.getProperty("jira.userName");
this.password = properties.getProperty("jira.password");
this.jiraUrl = properties.getProperty("jira.url");
this.restClient = getJiraRestClient();
}


private JiraRestClient getJiraRestClient() {
return new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(getJiraUri(), getUsername(), getPassword());
}

Please find the below error code.

Caused by: RestClientException{statusCode=Optional.of(401), errorCollections=[]}
at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:178)
at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:170)
at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:49)
at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:10)
at io.atlassian.util.concurrent.Promises$OfStage.lambda$fold$4(Promises.java:332)
at io.atlassian.util.concurrent.Promises.lambda$biFunction$7(Promises.java:422)
at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:822)
at java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:797)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
at java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962)
at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$1.lambda$doCompleted$0(SettableFuturePromiseHttpPromiseAsyncClient.java:37)
at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient.runInContext(SettableFuturePromiseHttpPromiseAsyncClient.java:61)
at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable.run(SettableFuturePromiseHttpPromiseAsyncClient.java:129)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 10 of

<html><head><title>Unauthorized (401)</title>

at org.codehaus.jettison.json.JSONTokener.syntaxError(JSONTokener.java:439)
at org.codehaus.jettison.json.JSONObject.<init>(JSONObject.java:169)
at org.codehaus.jettison.json.JSONObject.<init>(JSONObject.java:266)
at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient.extractErrors(AbstractAsynchronousRestClient.java:212)
at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:175)

1 answer

0 votes

Hi @Hari Hara Bhuvanesh ,

If the same code works fine for Jira Server but you are getting error "401 - unauthorized" in Jira Cloud, this can be because in Cloud basic authentication with username and password has been replaced by basic authentication with email address and API Token:

Atlassian has introduced support for API tokens for all Atlassian Cloud sites as a replacement for basic authentication requests that previously used a password or primary credential for an Atlassian account, as well as cookie-based authentication.

Basic authentication with passwords and cookie-based authentication are now deprecated and will be removed in 2019 in accordance with the Atlassian REST API policy.

 

Please replace username and password with email address and API Token and let us know if this solves the issue. 

 

Cheers,
Dario

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

303 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you