使用caldav导入谷歌日历

时间:2016-03-23 14:21:19

标签: google-calendar-api icalendar caldav

我无法将ics文件导入Google。 我搜索了日历apis。他们显然没有实现它。 然后我尝试了谷歌caldav(缺少99%的文档)。

我写了这个示例函数,它失败了,错误400 BAD REQUEST。

private void TestGoogleCaldav()
    {
        string contents = string.Empty;
        string downloadURL = string.Format("https://apidata.googleusercontent.com/caldav/v2/<email>/events/dav.ics");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(downloadURL));
        request.Method = "PUT";
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        request.ContentType = "application/x-www-form-urlencoded";
        request.KeepAlive = true;
        request.Timeout = 15 * 60 * 1000;

        string headervalue = string.Empty;
        string[] scope = new string[] { CalendarService.Scope.Calendar };
        string token = GetOauthAccessToken(scope);
        headervalue = string.Format("Authorization: Bearer {0}", token);
        request.Headers.Add(headervalue);

        string iCalFileName = @"C:\temp\invite.ics";
        System.IO.StreamReader myFile = new System.IO.StreamReader(iCalFileName);
        string myString = myFile.ReadToEnd();
        var bytes = Encoding.ASCII.GetBytes(myString);

        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }
        var response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
        }
        else
        {

        }
    }

ICS有效且未损坏。 (发送给外部用户时由Google生成)。

ICS档案:

BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20160427T150000Z
DTEND:20160427T160000Z
DTSTAMP:20160208T145831Z
ORGANIZER;CN=Test user:mailto:test@contoso.com
UID:nh9d4rkakmgg84f5rg3he8nq44@google.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=the user;X-NUM-GUESTS=0:mailto:bob@company.com
CREATED:20160208T145831Z
DESCRIPTION:View your event
LAST-MODIFIED:20160208T145831Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:ics
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

1 个答案:

答案 0 :(得分:0)

您的Content-Type标头必须为text/calendar。不知道这是否是唯一的问题,但它是最明显的问题。