Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,455,831
Community Members
 
Community Events
176
Community Groups

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

Edited

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

That does sound correct.

Username = email address

Password = PAT

and use Basic Authentication

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

AuthenticationHandler works!

Is jrjc with bearer authentication available in maven repository? 

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());

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