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 rest java client API return unauthorized 401 while getting issue

here is full stack trace

 

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:748)
Caused by: RestClientException{statusCode=Optional.of(401), errorCollections=[]}
at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.claim(DelegatingPromise.java:45)
at com.app.ConnectJira.main(ConnectJira.java:37)
... 6 more
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)
... 1 more
Caused by: org.codehaus.jettison.json.JSONException: A JSONObject text must begin 

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

<!--[if IE]><![endif]-->
<script type="text/javascript" >
var contextPath = '';
var DeferScripts = { deferState: 'disabled' };
</script>
<script>

ll">

<meta name="application-name" content="JIRA" data-name="jira" data-version="1001.0.0-SNAPSHOT">
</head>
<body id="jira" class="aui-layout aui-style-default page-type-message" data-version="1001.0.0-SNAPSHOT" >
<div class="aui-page-panel"><div class="aui-page-panel-inner">
<section class="aui-page-panel-content">
<header class="aui-page-header"><div class="aui-page-header-inner">
<div class="aui-page-header-main">
<h1>Unauthorized (401)</h1>
</div><!-- .aui-page-header-main -->
</div><!-- .aui-page-header-inner --></header><!-- .aui-page-header -->
<div class="aui-message aui-message-warning warning">
<p>Encountered a <code>&quot;401 - Unauthorized&quot;</code> error while loading this page.</p>
<p><a href="/secure/MyJiraHome.jspa">Go to Jira home</a></p>
</div>
</section><!-- .aui-page-panel-content -->
</div><!-- .aui-page-panel-inner --></div><!-- .aui-page-panel -->
</body>
</html>
at org.codehaus.jettison.json.JSONTokener.syntaxError(JSONTokener.java:463)
at org.codehaus.jettison.json.JSONObject.<init>(JSONObject.java:206)
at org.codehaus.jettison.json.JSONObject.<init>(JSONObject.java:313)
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)
... 15 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.516 s
[INFO] Finished at: 2019-05-05T21:20:15+05:00
[INFO] Final Memory: 14M/34M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default) on project PSR: An exception occured while executing the Java class. null: InvocationTargetException: org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 1 of <html>
[ERROR]
[ERROR] <head>
[ERROR] <title>Unauthorized (401)</title>
[ERROR] <meta name="application-name" content="JIRA" data-name="jira" data-version="1001.0.0-SNAPSHOT">
[ERROR] </head>
[ERROR] <body id="jira" class="aui-layout aui-style-default page-type-message" data-version="1001.0.0-SNAPSHOT" >
[ERROR] <div class="aui-page-panel"><div class="aui-page-panel-inner">
[ERROR] <section class="aui-page-panel-content">
[ERROR] <header class="aui-page-header"><div class="aui-page-header-inner">
[ERROR] <div class="aui-page-header-main">
[ERROR] <h1>Unauthorized (401)</h1>
[ERROR] </div><!-- .aui-page-header-main -->
[ERROR] </div><!-- .aui-page-header-inner --></header><!-- .aui-page-header -->
[ERROR] <div class="aui-message aui-message-warning warning">
[ERROR] <p>Encountered a <code>&quot;401 - Unauthorized&quot;</code> error while loading this page.</p>
[ERROR] <p><a href="/secure/MyJiraHome.jspa">Go to Jira home</a></p>
[ERROR] </div>
[ERROR] </section><!-- .aui-page-panel-content -->
[ERROR] </div><!-- .aui-page-panel-inner --></div><!-- .aui-page-panel -->
[ERROR] </body>
[ERROR] </html>

1 answer

1 accepted

1 vote
Answer accepted

Hello Sohail and welcome to the Community!

Thank you for sending over your error log. The main item I would focus on would be the 401 API response you received. This response is normally associated with permissions/access. Meaning the account or token you’re using does not have access to the issue you're attempting to retrieve. Things to check for to ensure you’re able to GET data via the API:

  1. Ensure the account/token used has permission to the project and may view issues
  2. Ensure the credentials are correct
  3. Ensure CAPTCHA has not been triggered for the user.

If you confirm the above and you’re still receiving a 401 response, please let us know the method/verb you’re attempting along with the endpoint you’re wanting to communicate with.

Regards,
Stephen Sifers

Hi Stephen,

I am also facing error 401.

 

I can create a ticket/issue via browser.

There is no CAPTCHA enabled when logged-in through browser.

I am using java api to connect to JIRA services to create issues. The URL being used is https://assurestart.atlassian.net/

The sample code is as below:

 

URI uri = new URI(property.getProperty("jira.url"));
final JiraRestClient jiraRestClient = new AsynchronousJiraRestClientFactory()
.createWithBasicHttpAuthentication(uri, property.getProperty("jira.username"),
property.getProperty("jira.password"));
jiraRestClient.getProjectClient().getProject("TA");
Promise<Project> projects = jiraRestClient.getProjectClient().getProject("HS");
System.out.println("Project name "+jiraRestClient.getProjectClient().getProject("TA").get().getName());

 

 

What I have observed is this code works intermittently. I remember it was working till mid of last week and it suddenly started throwing  this error. The behaviour is really unpredictable. It may work for some days and then may not work again for a few days.

Like Stephen Sifers likes this

Hello Amol and welcome to the Community!

Without seeing the 401 response, which is most likely unauthorized, it would be difficult to fully understand the error you're seeing. With this said, ensure you're using Username and API Token for authentication. Do not use your password in this case.

Further information on tokens may be found at API tokens.

Regards,
Stephen Sifers

Like # people like this

Issue resolved.

Thanks for taking time and respond, Stephen. I created API Token and tried with it and it magically worked. 

Thanks for your help :)

Hello Amol,

Very happy to hear it's working as expected now!

Regards,
Stephen Sifers

 

how to create API token in java code

Suggest an answer

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

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

888 views 7 24
Read article

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