I’m trying to create a JIRA plugin using the Atlassian Connect-Express framework. The add-on installs fine and I get the payload with the necessary keys. But I’m having issues with the JIRA REST API authentication.
When I try to make a GET request to fetch an issue, I get a 404 error saying the issue doesn’t exist or I don’t have permission. Here’s what I’m doing:
app.get('/fetch-issue', addon.authenticate(), function(req, res) {
const axios = require('axios');
axios.get('https://mycompany.atlassian.net/rest/api/2/issue/XYZ-123')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error:', error.response.status, error.response.data);
});
res.render('issue-details');
});
My app descriptor file includes the necessary scopes and authentication type:
{
"key": "my-jira-addon",
"authentication": {
"type": "jwt"
},
"scopes": [
"READ",
"WRITE"
]
}
I’m pretty sure I’m not doing this correctly. Should I be using OAuth instead? Or is there a way to make the JWT authentication work for the JIRA REST API calls?
hey guys, i had this same problem last week. the trick is to use the httpClient from the addon object instead of axios. it handles all the auth stuff for you. just do something like this:
const httpClient = addon.httpClient(req);
httpClient.get('/rest/api/2/issue/XYZ-123')
.then(response => {
// do stuff with response.body
})
that should fix it for ya. good luck!
I’ve encountered similar issues with JIRA REST API authentication in Atlassian Connect-Express plugins. The problem likely stems from not properly using the addon context for API requests. Instead of using axios directly, you should leverage the httpClient provided by the addon.
Try modifying your code like this:
app.get('/fetch-issue', addon.authenticate(), function(req, res) {
const httpClient = addon.httpClient(req);
httpClient.get('/rest/api/2/issue/XYZ-123')
.then(response => {
console.log(response.body);
res.render('issue-details', { issue: response.body });
})
.catch(error => {
console.error('Error:', error.statusCode, error.body);
res.status(500).send('Error fetching issue');
});
});
This approach ensures that the necessary authentication headers are automatically included in your API requests. It should resolve the 404 error you’re experiencing.
I’ve been down this road before, and it can be frustrating. Your approach is close, but there’s a key element missing. The Atlassian Connect-Express framework provides a built-in HTTP client that handles authentication for you.
Instead of using axios directly, try using the httpClient method from the addon object. It automatically includes the necessary JWT token in the request headers. Here’s how you can modify your code:
app.get('/fetch-issue', addon.authenticate(), function(req, res) {
const httpClient = addon.httpClient(req);
httpClient.get('/rest/api/2/issue/XYZ-123')
.then(response => {
console.log(response.body);
res.render('issue-details', { issue: response.body });
})
.catch(error => {
console.error('Error:', error.statusCode, error.body);
res.status(500).send('Error fetching issue');
});
});
This should resolve your authentication issues. Remember, when working with Atlassian Connect-Express, always use the provided httpClient for API calls to ensure proper authentication.