处理gapi.client.calendar.events.list的未捕获错误

时间:2017-11-03 03:28:54

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

我只是想问一下是否有办法捕获错误或者知道用户无法从日历中获取事件?我想要做的是添加注释,目前登录电子邮件没有访问事件的权限。一切正常,授权电子邮件可以访问这些事件。谢谢你的帮助。

这是我的代码:

 gapi.client.calendar.events.list({
      'calendarId': calendarid,
      'timeMin': (new Date()).toISOString(),
      'showDeleted': false,
      'singleEvents': true,
      'maxResults': 10,
      'orderBy': 'startTime'
    }).then(function(response) { 
      var events = response.result.items;
      //appendPre('Upcoming events:');

      if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;
          if (!when) {
            when = event.start.date;
          }
          //listingdata.push(event.summary+'('+when +'\n'+ event.description);

           //appendPre(event.summary + ' (' + when + ') ' + event.description)
        }
      } else {
         //appendPre('No upcoming events found.');
      }

    });

1 个答案:

答案 0 :(得分:0)

当您尝试访问不允许的资源时,每个Google API都必须返回代码403(禁止访问)。

所以,你只需要捕获http响应代码:

 gapi.client.calendar.events.list({
      'calendarId': calendarid,
      'timeMin': (new Date()).toISOString(),
      'showDeleted': false,
      'singleEvents': true,
      'maxResults': 10,
      'orderBy': 'startTime'
    }).then(function(response) { 
      var events = response.result.items;
      //appendPre('Upcoming events:');

      if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;
          if (!when) {
            when = event.start.date;
          }
          //listingdata.push(event.summary+'('+when +'\n'+ event.description);

           //appendPre(event.summary + ' (' + when + ') ' + event.description)
        }
      } else {
         //appendPre('No upcoming events found.');
      }

    }, function(error) {
          console.error("Execute error", error);
          if(error.code == 403)
          {
            console.log("No access")
          }

        });