Trouble with JIRA REST API auth in Atlassian Connect-Express plugin

I’m building a JIRA plugin using the Atlassian Connect-Express framework. The plugin installs fine and I get the expected payload. But I’m stuck trying to use the JIRA REST API with the JWT token from the framework.

When I make a GET request, I get a 404 error saying the issue doesn’t exist or I don’t have permission. Here’s my code:

app.get('/fetch-issue', addon.authenticate(), function(req, res){
  const axios = require('axios');
  axios.get('https://mycompany.atlassian.net/rest/api/2/issue/XYZ-1')
    .then(response => {
      console.log(response.status, response.data);
    })
    .catch(error => {
      console.error('Error:', error.message);
    });
  res.render('issue-details');
});

My app descriptor file includes the necessary scopes and authentication type. I’m pretty sure I’m missing something with the JWT setup. Any ideas on how to make this work? I’d prefer to stick with JWT rather than switching to OAuth if possible.

Has anyone successfully authenticated JIRA REST API calls in a similar setup? What am I overlooking?

I ran into a similar roadblock when I first started working with JIRA plugins. The key is to use the addon’s httpClient, as John_Clever mentioned, but there’s another crucial step.

Make sure you’re using the correct base URL. In your original code, you hardcoded ‘https://mycompany.atlassian.net’, which won’t work across different JIRA instances. Instead, leverage the context object:

app.get('/fetch-issue', addon.authenticate(), function(req, res) {
  const httpClient = addon.httpClient(req);
  const baseUrl = `${req.context.hostBaseUrl}/rest/api/2/issue/XYZ-1`;
  
  httpClient.get(baseUrl)
    .then(response => {
      // Handle response
    })
    .catch(error => {
      console.error('Error:', error);
    });

  res.render('issue-details');
});

This approach ensures your plugin works across different JIRA instances. Also, double-check your app descriptor for the ‘READ’ scope on issues. These tweaks should sort out your authentication woes.

hey man, i had a similar issue. try using the httpclient from the addon instead of axios. it handles the JWT stuff for you. something like this:

httpClient = addon.httpClient(req);
httpClient.get(‘/rest/api/2/issue/XYZ-1’)
.then(response => {
// do stuff with response
})

also double check ur app descriptor file. make sure u got the right scopes set up. good luck!

I’ve encountered similar issues when working with the JIRA REST API in Connect-Express plugins. The problem likely stems from how you’re making the API request. Instead of using axios directly, try utilizing the httpClient provided by the addon context. This automatically handles JWT authentication for you.

Here’s how you might modify your code:

app.get('/fetch-issue', addon.authenticate(), function(req, res) {
  const httpClient = addon.httpClient(req);
  httpClient.get('/rest/api/2/issue/XYZ-1')
    .then(response => {
      console.log(response.status, response.body);
    })
    .catch(error => {
      console.error('Error:', error.message);
    });
  res.render('issue-details');
});

This approach should resolve your authentication issues without needing to switch to OAuth. Remember to double-check your app permissions in the descriptor file as well.