Implementing Basic Auth for JIRA REST API in JavaScript: How to?

I’m working on a JavaScript app for a Smart TV to display JIRA dashboards. I need to fetch the dashboard list using the JIRA REST API. My problem is that the initial API call isn’t authenticated, so it returns a random list instead of the correct one.

I know I can use this URL to get the dashboards:

jira/rest/api/2/dashboard?startAt=&maxResults=

And this one to create a wallboard:

jira/plugins/servlet/Wallboard/?dashboardId=&os_username=&os_password=

The wallboard URL works fine because it includes authentication. But I need to authenticate the initial API call too.

I’ve seen examples using curl for basic auth, like this:

curl -D- -u username:password -X GET -H "Content-Type: application/json" http://example.com/rest/api/2/issue/createmeta

But I don’t know how to translate this to JavaScript. Can someone help me implement basic authentication for the JIRA REST API using JavaScript? It’s crucial that the solution is in JavaScript only. Thanks!

I’ve encountered similar issues when working with JIRA’s API. For JavaScript-only solutions, the fetch API is my preferred approach.

Here’s an example of how to use it:

const base64Credentials = btoa(`${username}:${password}`);
fetch('your_jira_url/rest/api/2/dashboard', {
  headers: {
    'Authorization': `Basic ${base64Credentials}`,
    'Content-Type': 'application/json'
  }
})
.then(response => response.json())
.then(data => {
  // Handle your dashboard data here
})
.catch(error => console.error('Error:', error));

This method encodes your credentials and includes them in the Authorization header. Ensure you are using HTTPS to secure the communication, and consider token-based authentication for enhanced security in production environments.

hey there! i’ve had some experience with jira api authentication. here’s a quick js snippet that might help:

let url = 'your_jira_url/rest/api/2/dashboard';
let creds = btoa('username:password');

fetch(url, {
  headers: {
    'Authorization': 'Basic ' + creds,
    'Content-Type': 'application/json'
  }
})
.then(r => r.json())
.then(data => console.log(data))
.catch(e => console.error(e));

hope this helps with your smart tv app!

I’ve dealt with JIRA API authentication before, and I can share what worked for me. Instead of using curl, you can leverage the Fetch API in JavaScript. Here’s a snippet that should do the trick:

const username = 'your_username';
const password = 'your_password';
const apiUrl = 'your_jira_url/rest/api/2/dashboard';

fetch(apiUrl, {
  method: 'GET',
  headers: {
    'Authorization': 'Basic ' + btoa(username + ':' + password),
    'Content-Type': 'application/json'
  }
})
.then(response => response.json())
.then(data => {
  console.log('Dashboards:', data);
  // Process your dashboard data here
})
.catch(error => console.error('Error fetching dashboards:', error));

This approach encodes your credentials and includes them in the Authorization header. Just remember to replace the placeholders with your actual JIRA instance URL and credentials. Also, make sure you’re using HTTPS to keep things secure. This method should work well for your Smart TV app to fetch the correct dashboard list.