我正在使用 expo 制作日历应用程序,并已使用 CALDAV 成功将其与 Google Calendar 同步。 现在我只想同步我的iCloud 日历。 我见过两种同步 iCloud 日历的解决方案。
这个模块看起来不错并且运行良好。但是,同步需要应用程序专用密码。用户会讨厌它,因为他们已经使用他们的 Apple 帐户 登录了应用程序。因此,我想使用 凭证令牌 等同步 iCloud 日历,而无需用户执行任何其他操作。 此外,CALDAV 没有日历的实时同步选项。它将需要一个计时器或类似的东西来检查更改的事件。
expo-calendar 与设备日历同步。因为我只制作 iOS 应用程序,所以这也很好。 但是,如果用户在 MAC PC 或 浏览器 上更新其 iCloud 日历,他们将需要打开 设备日历 才能与 我的应用程序 同步。 如果用户不打开 设备日历,iCloud 更改将不会同步到 设备日历,并且 expo-calendar 将无法从 设备日历获取更新的事件。
我看到一些 iPhone 应用程序可以很好地同步 iCloud 日历,无需用户在 Apple 登录后执行任何操作,例如“toggl track”等。
请帮助我找到另一个解决方案来同步Apple日历。 或者请告诉我克服以下问题的解决方案。
我从chatgpt 获得了示例代码。 但我在网上找不到任何有关 Rest API 的参考资料。 请让我知道这将解决我的问题。
const axios = require('axios');
// Your client ID and client secret obtained from the Apple Developer portal
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
// Function to authenticate and obtain an access token
async function authenticate() {
try {
const response = await axios.post('https://appleid.apple.com/auth/token', {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: 'https://api.apple.com/calendar',
});
const accessToken = response.data.access_token;
return accessToken;
} catch (error) {
console.error('Authentication failed:', error.response.data);
throw error;
}
}
// Function to fetch calendar events
async function fetchCalendarEvents(accessToken) {
try {
const response = await axios.get('https://api.apple.com/calendar/v1/events', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const events = response.data;
console.log('Calendar events:', events);
return events;
} catch (error) {
console.error('Failed to fetch calendar events:', error.response.data);
throw error;
}
}
// Example usage
async function main() {
try {
const accessToken = await authenticate();
const events = await fetchCalendarEvents(accessToken);
// Do something with the events
} catch (error) {
console.error('An error occurred:', error);
}
}
main();