You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过Google Calendar API获取原始ICS文件?JSON转ICS方法咨询

获取Google Calendar单个事件的ICS文件

Got it, let's break this down for you. The issue with your current code is that the Google Calendar API's events().get() method defaults to returning JSON data—so even though you're writing to an .ics file, the content is still JSON under the hood.

Luckily, you don't need to manually construct the ICS format from scratch. The API supports returning ICS directly by specifying the alt parameter in your request. Here's how to fix your code:

String eventId = "XXX";
String calendarId = "YYY";
OutputStream outputStream = new FileOutputStream("C:\\Users\\AAA\\Documents\\notepads\\output-text.ics");

// Add .setAlt("ics") to request the ICS format instead of JSON
calendarService.events().get(calendarId, eventId)
    .setAlt("ics")
    .executeAndDownloadTo(outputStream);

// Don't forget to close the stream to avoid resource leaks!
outputStream.close();

Why this works:

  • The alt parameter tells the API which format to return for the response. Setting it to "ics" triggers the API to generate and send the event data in the standard iCalendar (.ics) format.
  • Your original code omitted this parameter, so the API fell back to its default JSON response.

A quick side note: If you ever need to grab an entire calendar as an ICS file (not just a single event), you can use the calendar's public ICS feed URL. But for individual events, the above API approach with setAlt("ics") is the official, supported method.

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

火山引擎 最新活动