I’m using a google app script to get data on change and sending it to my backend. All of it is working correctly but now I want to add authentication meaning that the request is coming from the app script connected to the correct google cloud project.
here is my manifes scopes
"oauthScopes": [
"openid",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/spreadsheets.currentonly"
]
here is function
async function syncWithBackend(payload) {
const ui = SpreadsheetApp.getUi();
const options = {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
payload: JSON.stringify(payload),
};
try {
const res = UrlFetchApp.fetch('backend-url', options);
const responseText = res.getContentText();
ui.alert('Response from backend: ' + responseText);
} catch (error) {
ui.alert('Error communicating with backend: ' + error.message + ScriptApp.getIdentityToken()); // Handle errors
}
}
On my backend I check the following
const CLIENT_ID = ''
export async function verifyGoogleOAuthToken(token: string) {
const client = new OAuth2Client(CLIENT_ID)
try {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Ensure this matches your project's client ID
})
const payload = ticket.getPayload()
console.log('Verified payload:', payload)
// Check the email or project association if needed
if (payload.iss !== 'https://accounts.google.com') {
throw new Error('Invalid issuer')
}
return payload
} catch (error) {
console.error('Token verification failed:', error.message)
throw error
}
}
but I get the following error Wrong number of segments in token
Preferably I would like to use google service account to validate
what should I do?

