I’ve found complicated answers with JSON thingies but I think it should be easier than that?
I have a bash script that says
curl -u API_KEY:XXXXXXAPIKEY https://intervals.icu/api/v1/athlete/XXXXXID/activities.csv > intervals.csv
which works fine but what is the APPS Script equivalent of that?
Chat GPT says its
function downloadAndSaveCSV() {
const apiKey = ‘XXXXXAPIKEY’; // Your API key
const apiUrl = ‘https://intervals.icu/api/v1/athlete/XXXXID/activities.csv’; // Your CSV URL
const folderId = ‘1PtBTg6ARpsDYNFVoh0yAkcz_jADCCwBR’; // Your Google Drive folder ID
// Encode API key for Basic Authentication
const encodedApiKey = Utilities.base64Encode(apiKey + ‘:’);
const urlOptions = {
‘method’: ‘get’,
‘headers’: {
‘Authorization’: 'Basic ’ + encodedApiKey
},
‘muteHttpExceptions’: true
};
try {
// Fetch the CSV file from the API
const response = UrlFetchApp.fetch(apiUrl, urlOptions);
const responseCode = response.getResponseCode();
const csvContent = response.getContentText();
// Log response code and content
Logger.log('Response Code: ' + responseCode);
Logger.log('Response Content: ' + csvContent);
if (responseCode === 200) {
// Save the CSV content to Google Drive
const fileName = 'intervals.csv';
const folder = DriveApp.getFolderById(folderId);
folder.createFile(fileName, csvContent, MimeType.CSV);
Logger.log('CSV file saved successfully.');
} else {
Logger.log('Failed to fetch CSV: ' + response.getContentText());
}
} catch (error) {
Logger.log('Error: ’ + error.message);
}
}
but it doesn’t work