JIRA Java Rest client Api returns 401 on authentication.

Hari Hara Bhuvanesh M November 2, 2019

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

1 accepted

1 vote
Answer accepted
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 5, 2019

Hi @Hari Hara Bhuvanesh M ,

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

Hari Hara Bhuvanesh M January 1, 2020

Hi @Dario B 

Thanks for the reply. Sorry I missed the email notification of your response. I was thinking no one replied but just when had a look on this noticed your reply on my question.

It will be helpful if you share the steps that I need to follow to authenticate using API token. If possible please share the working piece of code in java that should be used for authentication.

Also I'm not sure on what to be entered in below section. Please advise.

OAuthCredentials.png

Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 7, 2020

@Hari Hara Bhuvanesh M ,

If you search the community you can find many threads with JAVA snippets showing hot to perform basic authentication like, for example:

 

The only difference is that instead of username and password you will use email address and API Token respectively.

 

For the rest, please notice that the provided screenshot has nothing to do with REST API authentication but is used to integrate with self hosted tools using Oauth.

 

For further details and in order to get a better understanding on this, please review the below documentation pages:

 

 

Cheers,
Dario

Hari Hara Bhuvanesh M January 13, 2020

@Dario B 

Thanks for the update. It worked like a charm...!!! Much Appreciated...

Like Dario B likes this
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 14, 2020

You are very welcome @Hari Hara Bhuvanesh M ! 

Can you kindly accept the answer so that this thread will be marked as resolved? 

 

Cheers,
Dario

Hari Hara Bhuvanesh M January 20, 2020

@Dario B 

Yes you can mark this as resolved... :)

Like Dario B likes this
Dhiraj Asrani December 30, 2021

Hi, i am using the email id (username) and API token (password) for authentication, which is correct as per the documentation.

Still see this issue.

RestClientException{statusCode=Optional.of(401), errorCollections=[]}] with root cause

org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 1 of Basic authentication with passwords is deprecated.

 

--------------------------------------excerpt from POM------------------------------

----------------------------------------------------------------------------------------

<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-api</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-core</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.4.0</version>
</dependency>

 

What am i missing here ?... Could you kindly guide ?

Dhiraj Asrani January 2, 2022

This stands resolved... 

Resolution:

1./ The URL was changed to https://<JIRA_BASE_URL> as opposed to https://<JIRA_BASE_URL>/rest/api/2/issue

2./ Correct value of the issue type (bug) is 10004l in my case.

Suggest an answer

Log in or Sign up to answer