Atlassian Jira - 401 only when using query paramet

2019-08-25 06:01发布

问题:

Currently working on a JIRA addon using the ACE framework. Executing a request using the integrated httpClient.

When I make a request such as this

https://instance.atlassian.net/rest/api/3/search

it works fine using the header Authorization: JWT <token> but when I run the same request with a query parameter like this

https://instance.atlassian.net/rest/api/3/search?maxResults=1

the request fails with a 401. I have confirmed that the JWT is not expired due to reverting the query parameters and seeing success again.

My atlassian-connect.json has scope READ as requested by the endpoint.

Any suggestions?

回答1:

I was surprised that the rest call "rest/api/2/search?maxResults=1" worked. But it did when I was logged into my instance.

If I try that as JQL in Issue Search (maxResults=1), I get an invalid or unauthorized error message.

My instance is on premise (API V2). Yours appears to be in the cloud (V3). So it may be that the REST search works more like the Issue Search in V3 and is therefore returning the 401

It's a guess that should be easy to test... replace your maxResults=1 with some actual JQL or a filter ID and see if your results change



回答2:

Since you're using ACE and utilizing httpClient, you might want to try the checkValidToken() route instead. The snippet below worked for me.

app.get('/mySearch', addon.checkValidToken(), function(req, res) {
    var httpClient = addon.httpClient(req);

    httpClient.get({
        url: '/rest/api/3/search?maxResults=1',
        headers: {
            'X-Atlassian-Token': 'nocheck',
            'Content-Type': 'application/json'
        }
    },
    function (err, httpResponse, body) {
        if (err) {
            return console.error('Search failed:', err);
        }

        console.log('Search successful:', body);
    });
});


标签: rest jwt jira