How do I use a personal access token (PAT) in jira-rest-java-client

Chao Wang November 10, 2021

Hi all,

I currently use basic authentication with password as below:

```

JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
URI jiraServerUri = baseUrl.toURI();
restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, config.getUsername(), config.getPassword());

```

How do I use a personal access token (PAT) in jira-rest-java-client ? What's the correct method to get a restClient with PAT ? 

 

Is that correct to replace username with email address, and password with PAT with the same createWithBasicHttpAuthentication call ?

 

Thanks.

1 answer

2 votes
Robert Wen_ReleaseTEAM_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2021

That does sound correct.

Username = email address

Password = PAT

and use Basic Authentication

Chao Wang November 10, 2021

Thank you for the reply.

It seems to be this must use Basic Authentication (based on https://bitbucket.org/atlassian/jira-rest-java-client/src/ecc435281cfd046f155996025fcd418033a7a13f/core/src/main/java/com/atlassian/jira/rest/client/auth/BasicHttpAuthenticationHandler.java#lines-44), however, we're moving to use PAT and stop using basic authentication for our Jira.

Is there a way to just use bearer authentication with JRJC ?

e.g. in cURL I can get access to Jira with:

 

curl -H "Authorization: Bearer <MyToken>" https://{JiraBaseUrl}/rest/api/content

 

Is that possible to achieve the same goal this in JRJC ?

 

==================================================================

Edit: it seems that I must create a AuthenticationHandler as:

 

public class BearerHttpAuthenticationHandler implements AuthenticationHandler {

private static final String AUTHORIZATION_HEADER = "Authorization";
private final String token;

public BearerHttpAuthenticationHandler(final String token) {
this.token = token;
}


@Override
public void configure(Builder builder) {
builder.setHeader(AUTHORIZATION_HEADER, "Bearer " + token);
}
}

 

and create with this authentication handler like:

 

BearerHttpAuthenticationHandler handler = new BearerHttpAuthenticationHandler(config.getToken());
JiraRestClient restClient= factory.createWithAuthenticationHandler(jiraServerUri, handler);

 

Like # people like this
maksimkovalev November 30, 2021

AuthenticationHandler works!

Alex June 17, 2022

Is jrjc with bearer authentication available in maven repository? 

Ishani June 19, 2022

What is the use of this part-

public void configure(Builder builder) {
builder.setHeader(AUTHORIZATION_HEADER, "Bearer " + token);
}

 

 

And I am facing difficulty in creating the handler by this method.

BearerHttpAuthenticationHandler handler = new BearerHttpAuthenticationHandler(config.getToken());
Chouhei Ngamba Ngangom August 18, 2022

Thanks @Chao Wang 

This helps! 

Created:

public class BearerHttpAuthenticationHandler implements AuthenticationHandler {

private static final String AUTHORIZATION_HEADER = "Authorization";
private final String token;

public BearerHttpAuthenticationHandler(final String token) {
this.token = token;
}


@Override
public void configure(Builder builder) {
builder.setHeader(AUTHORIZATION_HEADER, "Bearer " + token);
}
}
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
BearerHttpAuthenticationHandler handler = new BearerHttpAuthenticationHandler(<PAT>);
JiraRestClient restClient= factory.create(getJiraUri(), handler);

I just have to use factory.create(uri, handler) instead of 

factory.createWithAuthenticationHandler(jiraServerUri, handler);

Create Personal Access token 

Suggest an answer

Log in or Sign up to answer