Import / Export .ical in Google Calendar API

不问归期 提交于 2020-01-05 04:55:15

问题


I see there in the Web UI of Google Calendar that there is an option to download the .ical version of my calendar. I want to do this in my application I develop. I am looking over the Internet and in the documentation if there is something like that, but I can't find anything... Does the API provide this functionality? If yes, how can I start doing this?


回答1:


To make sure I understand your question, you wish to offer a "download as .ical" button on your web application, dynamically populated with the specific calendar event data from your application?

Think of an ical file (or more accurately, a .ics file) as just a string, but with a different Mime Type. The following describes the basics of the iCalendar format:

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

In ASP.NET, I'd recommend creating a handler (.ashx instead of .aspx) because it is more efficient if you don't need to serve a complete web page. In the handler, replace the ProcessRequest method with something like this (credit goes to 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();
}


来源:https://stackoverflow.com/questions/6605551/import-export-ical-in-google-calendar-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!