I have the following code, what it does is it uses the client id and client secret to get an access token, then uses the token to fetch a csv from our sql url, only issue is when I run the code I get an HTML pages code saved as a CSV: Any clue why this is happening and how I can fix it?
const express = require("express");
const routes = require("./routes");
const bodyParser = require('body-parser');
const qs = require('querystring')
const cron = require('node-cron');
const path = require('path');
const fs = require('fs');
const clientId = 'Removed for Security';
const clientSecret = 'Removed for Security';
const querySlug = 'ty9skkkgc4qk2b'; // Replace with your query slug
// Set the authentication API request URL and parameters
const authUrl = 'https://hawxservices.cloud.looker.com:19999/api/3.1/login';
const authParams = new URLSearchParams({
client_id: clientId,
client_secret: clientSecret,
});
// Authenticate with the Looker API and retrieve an access token
fetch(authUrl, {
method: 'POST',
body: authParams,
})
.then((response) => response.json())
.then((data) => {
const accessToken = data.access_token;
console.log(accessToken);
// Set the API request headers with the access token
const headers = {
Authorization: `Bearer ${accessToken}`,
};
// Set the API request URL to retrieve the query results as a CSV file
const resultsUrl = `https://hawxservices.cloud.looker.com/sql/${querySlug}?format=csv`;
// Make the API request to retrieve the query results as a CSV file
fetch(resultsUrl, {
headers: headers,
})
.then((response) => {
// Extract the response body as a string
return response.text();
})
.then((csvData) => {
// Save the file to the specified path
const filePath = 'C:/Users/Change Later/Documents/LookerCSVs/query_results.csv';
fs.writeFileSync(filePath, csvData, { encoding: 'utf8' });
console.log(`File saved to ${filePath}`);
})
.catch((error) => console.error(error));
})
.catch((error) => console.error(error));