在Google Calendar API中导入/导出.ical

时间:2011-07-07 03:41:09

标签: c# .net google-calendar-api icalendar

我在Google日历的网络用户界面中看到,可以选择下载我日历的.ical版本。我希望在我开发的应用程序中执行此操作。我正在查看互联网和文档中是否有类似的东西,但我找不到任何东西...... API是否提供此功能?如果是,我该如何开始这样做?

1 个答案:

答案 0 :(得分:4)

为了确保我理解您的问题,您希望在您的网络应用程序上提供“下载为.ical”按钮,并使用您应用程序中的特定日历事件数据进行动态填充?

将一个ical文件(或更准确地说,一个.ics文件)想象成一个字符串,但使用不同的Mime类型。以下描述了iCalendar格式的基础知识:

http://en.wikipedia.org/wiki/ICalendar

在ASP.NET中,我建议创建一个处理程序(.ashx而不是.aspx),因为如果您不需要提供完整的网页,它会更有效。在处理程序中,将ProcessRequest方法替换为类似的东西(信用转到http://webdevel.blogspot.com/2006/02/how-to-generate-icalendar-file-aspnetc.html

private string DateFormat
{
    get { return "yyyyMMddTHHmmssZ"; } // 20060215T092000Z
}

public void ProcessRequest(HttpContext context)
{
    DateTime startDate = DateTime.Now.AddDays(5);
    DateTime endDate = startDate.AddMinutes(35);
    string organizer = "foo@bar.com";
    string location = "My House";
    string summary = "My Event";
    string description = "Please come to\\nMy House";

    context.Response.ContentType="text/calendar";
    context.Response.AddHeader("Content-disposition", "attachment; filename=appointment.ics");

    context.Response.Write("BEGIN:VCALENDAR");
    context.Response.Write("\nVERSION:2.0");
    context.Response.Write("\nMETHOD:PUBLISH");
    context.Response.Write("\nBEGIN:VEVENT");
    context.Response.Write("\nORGANIZER:MAILTO:" + organizer);
    context.Response.Write("\nDTSTART:" + startDate.ToUniversalTime().ToString(DateFormat));
    context.Response.Write("\nDTEND:" + endDate.ToUniversalTime().ToString(DateFormat));
    context.Response.Write("\nLOCATION:" + location);
    context.Response.Write("\nUID:" + DateTime.Now.ToUniversalTime().ToString(DateFormat) + "@mysite.com");
    context.Response.Write("\nDTSTAMP:" + DateTime.Now.ToUniversalTime().ToString(DateFormat));
    context.Response.Write("\nSUMMARY:" + summary);
    context.Response.Write("\nDESCRIPTION:" + description);
    context.Response.Write("\nPRIORITY:5");
    context.Response.Write("\nCLASS:PUBLIC");
    context.Response.Write("\nEND:VEVENT");
    context.Response.Write("\nEND:VCALENDAR");
    context.Response.End();
}