Make authenticated request to Jira REST API with oauth access token

Deleted user July 3, 2020

Hi,

I have installed Jira software server on my computer. It's up and running on localhost:8080. I have an admin account on it.

My goal is to make requests to the api of that server, using node js.

I have followed this tutorial to get an access token that I should be able to use to make an authenticated request to the Jira REST API.

My problem is that I can't figure out the proper way to make API calls with that access token. Should I put it in the headers ? In the query parameters ?

I've worked with other APIs, for which I would do something like :

axios.get(jiraUrl +  '/rest/api/2/myself',
{
{
headers: {
'Authorization': `Bearer ${oauthAccessToken}`,
'Accept': 'application/json'
}
}
)
.then(result => console.log(result)
.catch(error => console.log(error)

 But this doesn't work with jira.

/rest/api/2/myself is supposed to return data about the logged in user, but I get a 401 error saying no user is logged in. What I understand from this is that I didn't provide the authentication token in the right way.

I have searched a lot of community discussions, but didn't manage to find how to properly make that request. Does anyone have a way to do this ?

Thanks.

1 answer

0 votes
Ryan Aquino December 5, 2020

Hello @[deleted] , I am also running into this issue. have you solved this issue ?

Deleted user December 7, 2020

Hi,

I ended up using jira-client.

It allows you to do pretty much everything, here's the full doc : https://jira-node.github.io/class/src/jira.js~JiraApi.html.

So basically, once you've got your accessToken and accessTokenSecret from oauth, you can call the following method to get a JiraCleint object, on which you can call any method from the doc.

function getJiraClient(accessToken, accessTokenSecret) {
  return new JiraClient({
    protocol: process.env.JIRA_PROTOCOL,
    host: process.env.JIRA_HOST,
    port: process.env.JIRA_PORT,
    oauth: {
      consumer_key: process.env.JIRA_CONSUMER_KEY,
      consumer_secret: PRIVATE_KEY,
      access_token: accessToken,
      access_token_secret: accessTokenSecret,
      signature_method: 'RSA-SHA1',
    },
    apiVersion: '2',
    strictSSL: false,
  });
}

 

Hope that helps!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events