Reputation: 691
Hi all I am trying to insert multiple events into google calendar (events count are 67) . Everything works good but I have got sometime error like
errors: [{domain: "usageLimits", reason: "rateLimitExceeded", message: "Rate Limit Exceeded"
I think that mean that I can not do so much calls.
I have write this code
var makeRequest = function(resource) {
console.log(resource);
var request = gapi.client.calendar.events.insert({
calendarId: 'primary',
resource: resource,
});
request.execute(function(resp) {
console.log(resp);
});
};
for (var j = 0; j < events.length; j++) {
makeRequest(events[j]);
}
I know that I need to use batch
like var batch = gapi.client.newBatch();
to avoid that problem but didn't understand how ... I looked here for some help Google Calendar javascript api - Add multiple events
Please help me to figure out how can I handle this problem.
Upvotes: 2
Views: 773
Reputation: 53
Yes you need something like gapi.client.newBatch()
for it o work, but before making the function call you need to set all of your events in a single event array say events
like this;
let eventData=['Your events data']
gapi.auth2
.getAuthInstance()
.signIn()
.then(() => {
var events = eventData.map((val) => {
return {
summary: `${val.name}`,
description: "description",
start: {
dateTime: val.timeStamp,
timeZone: 'timeZone,
},
end: {
dateTime: val.timeStamp,
timeZone: 'timeZone,
},
attendees: [
{ email: "[email protected]" },
{ email: "[email protected]" },
],
};
});
var batch = gapi.client.newBatch();
events.map((r, j) => {
batch.add(
//payloads
gapi.client.calendar.events.insert({
calendarId: "primary",
sendNotifications: true, //send notification to the attendees on the addition of an event to their calendar
resource: events[j],
})
);
});
batch.execute(function (event) {
let htmlLinks = Object.keys(event).map(
(key) => event[key].result.htmlLink
);
window.open(htmlLinks[0]);//display the first recently added event from the batch call
});
});
Upvotes: 0
Reputation: 116908
rateLimitExceeded is flood protection you are going to fast. You will need to slow your application down.
The apis limit your access by something called quota. There are user quotas and application quotas. In the image below you can see that my user based quota is 600 request per minute but my application based quota will be 10000 requests per minute.
Batching does not save you on quota as each request within the batch counts as a single request. In some cases sending batch requests will increase the chance that you will see rateLimitExceeded as the batching is going to fast. The only thing batching saves you on is the number of HTTP calls.
Upvotes: 2