I am trying to get the attendance not taken for today, but this script returns:
{"statusCode":401,"message":"The required Authorization header was missing or invalid, or the token has expired","status":401,"title":"The required Authorization header was missing or invalid, or the token has expired"}
Any advice?
Thank you very much!
SCript:
function fetchAndUpdateAttendanceData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Attendance Not Taken Today");
// Clear previous data except for the header row
if (sheet) {
var lastRow = sheet.getLastRow();
if (lastRow > 1) {
sheet.getRange(2, 1, lastRow - 1, sheet.getLastColumn()).clearContent();
}
}
// Correct the apiUrl construction (ensure correct concatenation)
var listId = 86555;
var apiUrl = 'https://api.sky.blackbaud.com/school/v1/lists/advanced/' + listId; // Ensure the list endpoint is correct
// Add your API subscription key here
var subscriptionKey = 'a06a7d071baa44xxxxxxx36b6984fb'; // Replace with your actual subscription key, keep it secure
// Set up headers for the API request with the correct key name
var options = {
'method' : 'GET',
'headers': {
'Bb-Api-Subscription-Key': subscriptionKey, // The correct header for Blackbaud's SKY API
'Content-Type': 'application/json'
},
'muteHttpExceptions': true // Allows full error response to be logged
};
// Fetch the attendance data
try {
var response = UrlFetchApp.fetch(apiUrl, options);
// Log the full response (will help identify exact error or confirm success)
Logger.log(response.getContentText()); // This will log the entire response, including error details
// Parse the response
var data = JSON.parse(response.getContentText());
// Assume 'data' is an array of attendance records (adapt this if structure differs)
var attendanceRecords = data.value || []; // Replace with actual response structure
// Populate the sheet with new data
if (attendanceRecords.length > 0) {
for (var i = 0; i < attendanceRecords.length; i++) {
var row = attendanceRecords[i];
sheet.appendRow([row.studentName, row.status, row.time]); // Replace with your fields
}
} else {
Logger.log('No attendance records found.');
}
} catch (e) {
Logger.log("Error fetching attendance data: " + e.toString());
}
}