You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展调用Google Calendar API无法获取新增事件求助

Google Calendar API Chrome扩展事件刷新异常问题

问题现象

  • 开发Chrome扩展,通过JavaScript调用Google Calendar API实现近期事件展示功能
  • 登录后可正常显示现有事件,但在Google Calendar网页端新增事件后,刷新扩展重新获取数据时,仅能加载旧事件;即使退出登录重新认证,新增事件仍无法显示
  • 该问题仅出现在部分日历中,新建的日历可正常刷新获取新增事件

已尝试的解决方法

  • 将登录时获取事件的函数作为刷新功能使用,但部分日历无法生效
  • 怀疑是缓存问题,在请求头中添加了'Cache-Control': 'no-cache',但问题仍未解决

当前事件获取函数代码

chrome.identity.getAuthToken({interactive: true}, function(token) {
    if (chrome.runtime.lastError) {
      return;
    }
  
    console.log("Authentication successful. Token:", token);
    isSignedIn = true;
    localStorage.setItem('gcal-signed-in',"true");
  
    var init = {
      method: 'GET',
      async: true,
      headers: {
        Authorization: 'Bearer ' + token,
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache'
      },
      'contentType': 'json'
    };

    const now = new Date();
    const isoNow = now.toISOString();
    const maxResults = 4;
  
    // 获取所有日历
    fetch(`https://www.googleapis.com/calendar/v3/users/me/calendarList`, init)
    .then(response => response.json())
    .then(calendarListData => {
      calendarIds = calendarListData.items.map(calendar => calendar.id);

      const fetchPromises = [];
      let events = new Map();
      // 获取每个日历的最新事件
      for (const calendarId of calendarIds) {
        const fetchPromise = fetch(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events?maxResults=${maxResults}&timeMin=${isoNow}`, init)
        .then((response) => response.json())
        .then((data) => {
          if (data.items) {
            data.items.forEach((event) => {
              const dateTimeParts = event.start.dateTime.split('T');
              const date = dateTimeParts[0];
              const time = dateTimeParts[1].substring(0, 5);
              const location = event.location; 
              console.log(location)
              
              // trim去除首尾空格和逗号
              events.set([date, time], [event.summary.trim(),location]);
            });
          } else {
            console.log("No upcoming events found.");
          }
        })
        .catch((error) => {
          console.error("Error fetching events:", error);
        });
        fetchPromises.push(fetchPromise);
      }
// 后续还有用Promise.all(fetchPromises)展示数据的代码

内容的提问来源于stack exchange,提问作者jessicayd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 23:17:31