0

I cannot manage to import an ics file to Google. I searched the Calendar apis. They apparently do not implement it. I then tried Google caldav (which lacks documentation at 99%).

I wrote this sample function and it fails with error 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
        {

        }
    }

The ICS is valid and not corrupted. (Generated by Google when sending to external user).

ICS file:

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
CloudAnywhere
  • 669
  • 1
  • 11
  • 27
  • 1
    "The ICS is valid and not corrupted. (Generated by Google when sending to external user).". Well, this is an iTIP request (METHOD:REQUEST), not a regular CalDAV payload. Also, ASCII? Really? – hnh Mar 23 '16 at 20:26
  • I'm opened to try to import a regular calDAV payload. Do you have a link to a test one? ( Note: this file imports ok into exchange and amazon workmail). – CloudAnywhere Apr 06 '16 at 06:53
  • Just capture one from iCal using Charles or a similar tool. – hnh Apr 06 '16 at 07:17

1 Answers1

0

Your Content-Type header needs to be text/calendar. Don't know if that's the only problem, but it's the most obvious one.

Evert
  • 93,428
  • 18
  • 118
  • 189