I built a task manager chat app using apps scripts. It initially used user authentication to run the app but later I realized that I cannot give access to the users to the underlying spreadsheet where the app is supposed to store the task details. So I realized the app should be authenticated as a service account. I have already created a service account and modified my script but it seems the app is still requesting for user authentication by showing the consent screen. Here is a snippet of my code, I have a hunch that the way I am interacting with google apis needs to change? If so, it will be a lot of change in my appscript. Is there a quicker workaround?
function onAddToSpace(event) {
const oauth2Client = getOAuth2Client();
// Check if the OAuth2 client is authorized
if (!oauth2Client.hasAccess()) {
// Handle the case when authorization is missing (e.g., redirect user to authorize)
return { text: "Authorization required. Please authorize the app." };
}
let message = '';
// Personalizes the message depending on how the Chat app is called.
if (event.space.singleUserBotDm) {
message = `Hi ${event.user.displayName}!`;
} else {
const spaceName = event.space.displayName ? event.space.displayName : "this chat";
message = `Hi! Thank you for adding me to ${spaceName}.`;
}
// Lets users know what they can do and how they can get help.
message = message +
' To learn what I can do, type /help*.'
return { "text": message };
}
function fetchTaskById(taskId) {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheetByName(task_sname)
const tasks = sheet.getDataRange().offset(1,0).getValues()
const task = tasks.find(function(dataRow){
return dataRow[idx_task_id] == taskId
})
return task ? createTaskObject(task) : null; // Return the task if found, otherwise return null
}