Get attachments from API using NODE and getting error 401

Ronaldo April 15, 2023

I am trying to download the contents of an attachment from a issue with nodejs. But I always get - Unauthorized (401)

But I do with the same user and token several other calls successfully. is I have the same header in the call. see code below:

 

async function baixarArquivoXls2(id) {
  try {
    const headers = {
      'Authorization': `Basic ${Buffer.from(conection.username + ":" + conection.password
        // 'email@example.com:<api_token>'
      ).toString('base64')}`,
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    };
    const response = await axios.get(conection.urlbase+'/secure/attachment/'+id,  { headers });
    console.error('OK');
  } catch (error) {
    console.error('Erro:', error);
  }
}
I Tryed by this way too. But get a 404 bad request
null for uri: <mylink>/rest/api/2/attachment/content/24200'
async function baixarArquivoXls3(id) {
  try {
    const headers = {
      'Authorization': `Basic ${Buffer.from(conection.username + ":" + conection.password
        // 'email@example.com:<api_token>'
      ).toString('base64')}`,
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    };
    const response = await axios.get(conection.urlbase+'/rest/api/2/attachment/content/'+id,  { headers });
    console.error('OK');
  } catch (error) {
    console.error('Erro:', error);
  }
}
Where are my error? anybody knows?

1 answer

0 votes
Ian Carlos
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2023

Try this

Create a Jira Token for your user, this link will explain: Manage API tokens for your Atlassian account (and it will guide you to this link API Tokens)

Save the token

Modify your code to

const headers = {
      'Authorization': 'Token the_token_you_just_created'
      ...
and
await axios.get(conection.urlbase+'/secure/attachment/'+id,  { headers: headers, auth: {username: your_jira_user_email, password: the_token_you_just_created } });

 

Let me know if this worked for you

 

Good luck!

Ronaldo April 18, 2023

conection.username and conection.password stores exactly these values.

Ian Carlos
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2023

Okay, but, have you tried modifying the code the way I told you? changing the Auhtorization in the header and set the auth like another param

Just in case, test it

 

In Python worked for me applying these changes

Suggest an answer

Log in or Sign up to answer