ICloud Caldav连接获取日历

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ICloud Caldav连接获取日历相关的知识,希望对你有一定的参考价值。

我正在尝试连接到ICloud日历并解析我可以执行的事件,如果我使用公共日历网址并且它工作正常。但是这样做我无法更新事件,因为我需要CalDav连接。

我很挣扎,因为这里的大多数示例都返回404 Not Found。

任何人都可以在C#中为我提供“ICloud Caldav连接以获取日历”的示例或工作示例。

请记住上面的内容我将只有UsernamePassword帐户,所以我必须加载日历列表。

答案

找到this文章非常有助于完成任务:

示例代码:

static string calendarURI = "https://caldav.icloud.com/";
    static string username = "abc@icloud.com";
    static string password = "xxxx-xxxx-xxxx-xxxx";
    //static string content = "<?xml version="1.0" encoding="utf-8"?><propfind xlmns="DAV:"><allprop/></propfind>";
    static string content = "<propfind xmlns='DAV:'><prop><current-user-principal/></prop></propfind>";

public async static void GetCalendars()
    {

        try
        {
            ResponseStream = ExectueMethod(username, password, calendarURI, "PROPFIND", null, content, "application/x-www-form-urlencoded","0");

            XmlDocument = new XmlDocument();
            XmlDocument.Load(ResponseStream);
            string xmlInner = XmlDocument.InnerXml;

            XmlDocument innerXmlDocument = new XmlDocument();
            innerXmlDocument.LoadXml(xmlInner);

            XmlNode statusCheck = innerXmlDocument.GetElementsByTagName("status")[0];
            if (statusCheck != null)
            {
                string status = statusCheck.InnerText.Trim();
                if (status == "HTTP/1.1 200 OK")
                {
                    XmlNode tag = innerXmlDocument.GetElementsByTagName("current-user-principal")[0];
                    if (tag != null)
                    {
                        XmlNode taghref = tag.ChildNodes[0];
                        if (taghref != null)
                        {
                            string href = taghref.InnerText.Trim();

                            string baseUrl = "https://caldav.icloud.com" + href;

                            Console.WriteLine("BASSE URL :" + baseUrl);
                            content = "<propfind xmlns='DAV:' xmlns:cd='urn:ietf:params:xml:ns:caldav'><prop><cd:calendar-home-set/></prop></propfind>";
                            ResponseStream = ExectueMethod(username, password, baseUrl, "PROPFIND", null, content, "application/x-www-form-urlencoded","0");

                            XmlDocument = new XmlDocument();
                            XmlDocument.Load(ResponseStream);
                            xmlInner = XmlDocument.InnerXml;

                            innerXmlDocument = new XmlDocument();
                            innerXmlDocument.LoadXml(xmlInner);
                            tag = innerXmlDocument.GetElementsByTagName("calendar-home-set")[0];
                            if (tag != null)
                            {
                                taghref = tag.ChildNodes[0];
                                if (taghref != null)
                                {
                                    string calURL = taghref.InnerText.Trim();
                                    Console.WriteLine("CALENDER URL :" + calURL);
                                    content = "<propfind xmlns='DAV:'><prop><displayname/><getctag />calendar-data</prop></propfind>";

                                    ResponseStream = ExectueMethod(username, password, calURL, "PROPFIND", null, content, "application/x-www-form-urlencoded","1");
                                    XmlDocument = new XmlDocument();
                                    XmlDocument.Load(ResponseStream);
                                    xmlInner = XmlDocument.InnerXml;

                                    innerXmlDocument = new XmlDocument();
                                    innerXmlDocument.LoadXml(xmlInner);
                                    int xx = 0;

                                    foreach (XmlNode row in innerXmlDocument.GetElementsByTagName("response"))
                                    {
                                        if (xx > 0)
                                        {
                                            string calhrefUrlfinal = row.InnerXml.Remove(row.InnerXml.IndexOf("</href>"));
                                            calhrefUrlfinal = calhrefUrlfinal.Replace("<href xmlns="DAV:">", "");
                                            string Calname = row.InnerXml.Remove(row.InnerXml.IndexOf("</displayname>"));
                                            Calname = Calname.Remove(0, Calname.IndexOf("<displayname>")).Replace("<displayname>", "").Trim();

                                            string statusx = row.InnerXml.Remove(row.InnerXml.IndexOf("</status>"));
                                            statusx = statusx.Remove(0, statusx.IndexOf("<status>")).Replace("<status>", "").Trim();

                                            if (statusx == "HTTP/1.1 200 OK")
                                            {
                                                Console.Write("THIS IS VALID CAL!");
                                                // ADD THIS CAL TO LIST TO SHOW!
                                                content = "<propfind xmlns='DAV:'><prop><calendar-data/><getctag /></prop></propfind>";

                                                ResponseStream = ExectueMethod(username, password, calhrefUrlfinal, "PROPFIND", null, content, "application/x-www-form-urlencoded", "1");
                                                XmlDocument = new XmlDocument();
                                                XmlDocument.Load(ResponseStream);
                                                xmlInner = XmlDocument.InnerXml;

                                                XmlDocument icsSoc = new XmlDocument();
                                                icsSoc.LoadXml(xmlInner);

                                                foreach (XmlNode ics in icsSoc.GetElementsByTagName("href"))
                                                {
                                                    string t = ics.InnerText.Trim();
                                                    if (t.Contains(".ics"))
                                                    {
                                                        DownloadICS(t, Calname + ".ics");
                                                        break;
                                                    }
                                                }

                                            }

                                        }
                                        xx = xx + 1;
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        { }
    }

       private static void DownloadICS(string pathUri, string fileNames)
    {
        WebClient request = new WebClient();
        request.Credentials = new NetworkCredential(username, password);

        Byte[] data = request.DownloadData(pathUri);
        var str = System.Text.Encoding.Default.GetString(data);
        string path = "icloudCalenders\" + fileNames.Substring(fileNames.LastIndexOf(" / ") + 1);

        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        fs.Write(data, 0, data.Length);
        fs.Close();

    }

private static Stream ExectueMethod(string username, string password, string caldevURI, string methodName, WebHeaderCollection headers, string content, string contentType,string depth)
    {
        HttpWebRequest httpGetRequest = (HttpWebRequest)WebRequest.Create(caldevURI);
        httpGetRequest.Credentials = new NetworkCredential(username, password);
        httpGetRequest.PreAuthenticate = true;
        httpGetRequest.Method = methodName;
        httpGetRequest.Headers.Add("Depth", depth);
        httpGetRequest.Headers.Add("Authori

以上是关于ICloud Caldav连接获取日历的主要内容,如果未能解决你的问题,请参考以下文章

CalDAV 请求 gGt iCloud 日历事件

从 iCloud 获取 CalDAV 事件返回“403 Forbidden”

iCloud日历删除事件请求不适用于caldav

如何从 Caldav 客户端为 iCloud 日历删除重复的事件条目

CALDAV 在 icloud 中添加带有注释的事件

是否有 C++ CalDav 协议库可以连接到 iCloud//Yahoo [关闭]