Skip to content

Instantly share code, notes, and snippets.

@BorisKozo
Created April 2, 2017 09:06
Show Gist options
  • Save BorisKozo/8753731e28008dc07b35a717963de351 to your computer and use it in GitHub Desktop.
Save BorisKozo/8753731e28008dc07b35a717963de351 to your computer and use it in GitHub Desktop.
Getting Tableau data from Node.js
//We use this code to retrieve data directly from Tableau server 10.1 from our Node.js server
//This is just a simplified example, not the actual code :)
// You need to fill in all the things in < >
const tableauServer = 'https://<IP OF YOUR SERVER>/trusted';
const username = '<username of a valid user to view the data>';
const reportPath = '/views/<WORKBOOK>/<view>';
const request = require('request-promise-native');
//This is optional, you can add filter parameters directly into the URL
//Check if you need to URI encode the parameter value
//You can have more than one parameter, this is a regular query string
const parameters = `${encodeURIComponent('<Parameter name>')}=<Paramter value>`;
//Note, you will need to configure your server to accept trusted connection from the IP of the server running this code.
//See this: https://onlinehelp.tableau.com/current/server/en-us/trusted_auth.htm
function getTrustedTicket(server, username) {
return request({
method: 'POST',
uri: `${server}?username=${username}`,
rejectUnauthorized: false
})
.then((ticket) => {
if (ticket.toString() === '-1') {
const errorMessage = 'Tableau server did not return a ticket!';
throw new Error(errorMessage);
}
return ticket;
});
}
getTrustedTicket(tableauServer, username)
.then((ticket) => {
//Note that I ask for the data as CSV to avoid all the iFrame shananigans
const vizUrl = `${tableauServer}/${ticket}${reportPath}.csv?${parameters}`;
return request({
method: 'GET',
uri: vizUrl,
rejectUnauthorized: false
});
})
.then((csvData) => {
res.send(csvData);
})
.catch((err) => {
console.log(err);
res.send();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment