问题是
我正在try 使用JavaScript在我的Chrome扩展上包含即将到来的Google Calendar活动的简单显示,所以我一直在使用Google Calendar API.问题是,一旦我登录,即将到来的事件就会正确显示,但如果我在Gcal上添加事件(不是通过Chrome扩展,而是在Gcal本身上),并try 重新获取新事件,则只会显示旧事件.即使我注销并重新进行身份验证,新事件也永远不会出现.
奇怪的是,这只发生在我的一些日历上,我注意到的一件事是,如果我完全创建了一个新日历,刷新确实会获取新的事件.
我所try 的
作为一名开发人员,我对使用Google API非常陌生,所以任何建议或方向都会有所帮助!
基本上,我有一个功能,可以在你登录后从Gcal获取事件,我想这也可以作为我的刷新按钮,因为它应该重新获取所有的数据?然而,当我测试它时,它在我的Gcal上的一些日历上不起作用.我认为这可能是缓存的问题,所以我try 删除缓存/在头文件中包含此标记'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; // getting all calendars 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(); // getting most top recent events of each calendar 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 gets rid of leading and trailing white space/commas 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); } // a bit more 编码 after this to display the data using Promise.all(fetchPromises) 编码>
谢谢你!如有任何帮助,我们不胜感激