Forums

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

How can I use JWT in NodeJs for my app?

Rui Costa
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 12, 2020

Hi Community,

I am trying to connect my nodejs app to my jira instance, where I have my app installed.

I have saved all the tenant information on a json file "tenant.json", but when I try to generate a JWT to query my instance I am getting a "401 Unauthorized"

 

This is the code:

var jwt = require('atlassian-jwt');
var moment = require('moment');
var fetch = require('node-fetch');
var tenant = require('./tenant.json');


issue = '10001';const myreq = jwt.Request = jwt.fromMethodAndUrl('GET',  '/rest/api/2/issue/' + issue);

const now = moment().utc();

const
 tokenData = {
"iss": tenant.clientKey,
"iat": now.unix(),
"exp": now.add(3'minutes').unix(),
"qsh": jwt.createQueryStringHash(myreq)
};

const token = jwt.encode(tokenDatatenant.sharedSecret);

fetch
(tenant.baseUrl + '/rest/api/2/issue/' + issue,
{
method: 'GET',
headers: {
'Authorization': 'JWT ' + token,
'Accept': 'application/json'
}
}).then(response => {
console.log(`Response: ${response.status} ${response.statusText}`);
}).catch(err => console.error(err));

Can someone help?

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events