I’m using Google Calendar API to update google calendar events by using batch request I can perform delete and insert and update operations using batch request but when I use patch method using batch request im getting 403 RateLimit error, not sure why, other methods works fine in batch request but not in case for PATCH method.
I also used Exponential Backoff method still not working, this is the code I used.
Thank you for your Time.
try {
const chunkSize = 100; // Google Calendar API allows a maximum of 1000 request to be processed at once
let retryDelay = 1000; // Start with a 1 second delay
for (let i = 0; i < eventsToDelete.length; i += chunkSize) {
const chunk = eventsToDelete.slice(i, i + chunkSize);
let retries = 0;
const maxRetries = 5;
while (retries < maxRetries) {
try {
// Create a batch request to delete all events in the chunk
const boundary = `batch_${uuidv4()}`;
let body = '';
chunk.forEach(event => {
body +=
`--${boundary}rn` +
'Content-Type: application/httprn' +
`Content-ID: <${event.id}>rnrn` +
`DELETE /calendar/v3/calendars/${calendarId}/events/${event.id} HTTP/1.1rn` +
`Authorization: Bearer ${oAuth2Client.credentials.access_token}rnrn`;
});
body += `--${boundary}--`;
const response = await axios.post('https://www.googleapis.com/batch/calendar/v3', body, {
headers: {
'Content-Type': `multipart/mixed; boundary="${boundary}"`,
Authorization: `Bearer ${oAuth2Client.credentials.access_token}`,
},
});
if (response.status === 200) {
// If successful, reset the retry delay
retryDelay = 1000;
break;
}
} catch (error) {
console.log('Error from deleting this and follwing events', error.message);
// If rate limited, wait for the retry delay then double it
if (error.message === 'Rate Limit Exceeded') {
await new Promise(resolve => setTimeout(resolve, retryDelay));
retryDelay *= 2;
retries++;
} else {
// If the error is something else, rethrow it
throw error;
}
}
if (retries === maxRetries) {
throw new Error('Max retries reached');
}
}
}