【问题标题】:add multiple events in google calendar witch batch在谷歌日历女巫批处理中添加多个事件
【发布时间】:2021-12-28 11:16:24
【问题描述】:

大家好,我正在尝试将多个事件插入谷歌日历(事件数为 67)。一切正常,但我有时会遇到错误,例如

     errors: [{domain: "usageLimits", reason: "rateLimitExceeded", message: "Rate Limit Exceeded"

我认为这意味着我不能打那么多电话。

我已经写了这段代码

        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]);
          }

我知道我需要像 var batch = gapi.client.newBatch(); 一样使用batch 来避免该问题,但不明白如何...我在这里寻求帮助Google Calendar javascript api - Add multiple events

请帮我弄清楚我该如何处理这个问题。

【问题讨论】:

    标签: javascript google-api google-calendar-api google-api-js-client


    【解决方案1】:

    rateLimitExceeded 是您要禁食的防洪措施。您需要降低应用程序的速度。

    api 通过称为配额的东西限制您的访问。有用户配额和应用程序配额。在下图中,您可以看到我的基于用户的配额是每分钟 600 个请求,但我的基于应用程序的配额是每分钟 10000 个请求。

    批处理不会为您节省配额,因为批处理中的每个请求都算作一个请求。在某些情况下,发送批处理请求会增加您看到 rateLimitExceeded 的机会,因为批处理会很快。批处理为您节省的唯一一件事是 HTTP 调用的数量。

    【讨论】:

      【解决方案2】:

      是的,您需要gapi.client.newBatch() 之类的东西才能正常工作,但在调用函数之前,您需要将所有事件设置在一个事件数组中,比如events,就像这样;

      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: "attendee1@mail.com" },
                      { email: "attendee2@mail.com" },
                    ],
                  };
                });
                
              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
                });
              });
                
                

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多