2016-03-23 58 views
0

我無法設法導入ics文件到谷歌。 我搜索了日曆apis。他們顯然沒有實現它。 然後我嘗試了Google caldav(它缺少99%的文檔)。導入ics到谷歌日曆使用caldav

我寫了這個示例函數,它失敗了,出現錯誤400錯誤請求。

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:[email protected] 
UID:[email protected] 
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=the user;X-NUM-GUESTS=0:mailto:[email protected] 
CREATED:20160208T145831Z 
DESCRIPTION:View your event 
LAST-MODIFIED:20160208T145831Z 
LOCATION: 
SEQUENCE:0 
STATUS:CONFIRMED 
SUMMARY:ics 
TRANSP:OPAQUE 
END:VEVENT 
END:VCALENDAR 
+1

「ICS有效且沒有損壞(發送給外部用戶時由Google生成)」。那麼,這是一個iTIP請求(METHOD:REQUEST),而不是一個常規的CalDAV有效載荷。另外,ASCII?真? – hnh

+0

我打開試圖導入一個常規calDAV有效載荷。你有鏈接到測試嗎? (注意:這個文件導入交換和亞馬遜工作郵箱)。 – CloudAnywhere

+0

只需使用Charles或類似工具從iCal中捕獲一個。 – hnh

回答

0

您的Content-Type頭必須text/calendar。不知道這是唯一的問題,但它是最明顯的問題。