You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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());
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.