Forums

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

Issue while using token in creating Jira through REST API

Shubham July 13, 2022

Hey guys

I am using exactly same java code for creating jira ticket using username:password and username:api_token.

I am able to create ticket using username:password (ofcourse encoded with base64) but when I am using the same code for api token , it is giving me 401 unauthorized - no body error with restTemplate.

 

Please suggest me the solution for this.

 

Thanks

1 answer

0 votes
Fabio Racobaldo _Catworkx_
Community Champion
July 13, 2022

Hi @Shubham ,

welcome to the Atlassian community!

Please could you share your REST API call (without user info) so that we can support you on that? How did u generate token for user?

Fabio

Shubham July 13, 2022

Hey @Fabio Racobaldo _Catworkx_ 

Yeah I have created token from my account profile>Personal Access token  and using the same while authorization.

I am using something like this.. with proper url,username and api token.

 

public HttpHeaders createHeadersWithAuthentication()
{
String plainCreds = "myusername" + ":" + "api_token";
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
String base64Creds = new String(base64CredsBytes);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Authorization", "Basic " + base64Creds);
return requestHeaders;
}

httpHeaders = createHeadersWithAuthentication();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
String url = "https://onejira.******.com/rest/api/2/issue";
HttpEntity<String> requestEntity = new HttpEntity<String>(jiraRequest.toString(), httpHeaders);
String jiraResponse = restTemplate.postForObject(url ,requestEntity, String.class);

 

When I am replacing 

String plainCreds = "myusername" + ":" + "api_token";

with 

String plainCreds = "myusername" + ":" + "mypassword";

It is working perfectly fine for me with the same lines of code.. even with postman i tried using token instead of password , both are working fine , only issue is with using token inside code.

 

Please help. Let m know if you require anything else from my side.

 

Thank You

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events