jira-node / node-jira-client

A Node.js wrapper for the Jira REST API
https://jira-node.github.io/
MIT License
450 stars 169 forks source link

Cannot connect to JIRA with NodeJS #310

Closed dimisizz closed 3 years ago

dimisizz commented 3 years ago

This is my code:

const jiraApi = require('jira-client');

var jira = new jiraApi({protocol: 'https',
     host: 'jira.XXXX.global',
     username: 'myUsername',
     password: 'myPassword',
     apiVersion: '2'});

jira.findIssue('myIssueNumber').then(() => {
     console.log(`Status: ${issue.fields.status.name}`
)}).catch((error) => {
     console.log(error)});

I expect the program to print issue.fields.status.name in my terminal but I encounter Request failed with status code 401.

I am sure my username and password are correct because I can access JIRA application via chrome and I can see the detail of this IssueNumber.

I don't know why I encounter 401 error. Can anyone help me?

mtjandra-applyboard commented 3 years ago

What password are you using? You may need to specify an API token

Specify a password for this tool to authenticate all requests with. Cloud users need to generate an API token for this value.

https://confluence.atlassian.com/cloud/api-tokens-938839638.html

dimisizz commented 3 years ago

@mtjandra-applyboard I tried using the password I used to login JIRA via chrome and tried using API token generated from https://id.atlassian.com/manage/api-tokens. Nothing is working. I think my JIRA is not cloud version. I think it is server version.

fhiutc commented 3 years ago

You may forget add strictSSL: false if you are using self-hosting jira.

BenAlderfer commented 3 years ago

I am also attempting to authenticate using an api token and I have not had success. I'm on jira cloud and getting a deprecation error: 401 - "Basic authentication with passwords is deprecated. For more information, see: https://confluence.atlassian.com/cloud/deprecation-of-basic-authentication-with-passwords-for-jira-and-confluence-apis-972355348.html\n"

When directly calling the api, I can just set this header: Authorization: 'Basic ${apiToken}', is there an equivalent option I could pass here? I noticed that using the api token as a password also requires I add a username which should not be needed

pioug commented 3 years ago

I noticed that using the api token as a password also requires I add a username which should not be needed

@BenAlderfer Not sure where this assumption comes from but the documentation clearly says that the user's email is needed. See https://confluence.atlassian.com/cloud/api-tokens-938839638.html

image

BenAlderfer commented 3 years ago

@pioug I was referring to basic auth which actually does have the username in it, but its encoded with the token into a single string: https://developer.atlassian.com/cloud/jira/platform/basic-auth-for-rest-apis/#supplying-basic-auth-headers

pioug commented 3 years ago

@BenAlderfer Oh ok. No, I don't think it's possible to set directly the authorization header.

HrithikRaj1999 commented 2 months ago

This is working for me for jira-client@^8.2.2

exports.loginJira = async function (username, password, bearerToken) {
  let jiraObj = {
    protocol: "https",
    host: someObject.jira.url,
    apiVersion: "2",
    strictSSL: true,
    bearer: bearerToken,
  };
  if (bearerToken) {
    jiraObj.bearer = bearerToken;
  } else {
    jiraObj.username = username;
    jiraObj.password = password;
  }

  let jira = new JiraApi(jiraObj);

  return jira
    .getCurrentUser()
    .then((result) => {
      console.log("Login Success");
      return { result, message: "Login successful", success: true };
    })
    .catch((err) => {
      console.log("Login Failed Check creds");
      return { err, message: "Login unsuccessful", success: false };
    });
};