且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apple iCloud 访问 uwp 应用程序中的联系人

更新时间:2023-12-03 18:24:34

因此,如果您只想获取联系人(并以 JSON 格式),我之前的答案可能是可行的方法.但是,由于我希望能够在Apple帐户上执行CRUD操作,因此更好的方法是使用icloud/apple支持的CARDDAV协议.

  1. 使用基本身份验证获取主要用户(此基本身份验证中使用的密码必须作为应用密码生成此处) 通过在 https://contacts.icloud.com 发出 PROPFIND 请求与请求内容:

<道具><当前用户委托人/></propfind>

  1. 上一步将让您检索格式为/1437425399/principal/的原则.现在,您可以在链接 https://contacts 上为该用户启动 PROPFIND 查询家庭集.icloud.com/1437425399/principal 请求内容如下:

<道具><c:addressbook-home-set/></propfind>

  1. 根据之前的请求,您将获得格式为 https://p48-contacts.icloud.com:443/1437425399/carddavhome/.您可以在 home-set 链接中使用以下 PROPFIND 请求来查询用户的 vcard 存在于何处:

<道具><资源类型/></propfind>

  1. 您将收到放置所有卡片的位置(例如/1437425399/carddavhome/card/).使用地址簿查询,现在您可以在上一个链接中收到的端点发起 REPORT 请求:

<道具></c:addressbook-query>

  1. 这将为您提供标签中的所有卡片.然后,您可以使用 addressbook-multiget REPORT 请求获取多张卡片:

<道具><getetag/><c:地址数据/><href>/1437425399/carddavhome/card/somecard.vcf</href><href>/1437425399/carddavhome/card/anothercard.vcf</href></c:addressbook-multiget>

为了更新、创建和删除卡片,您可以在/1437425399/carddavhome/card/cardtobemanipulated.vcf"端点使用我们之前讨论的基本身份验证在将 Content-Type 设置为text/vcard"后触发 PUT 请求并发送内容中的 VCard 以进行更新和创建请求.

I am trying to access my iCloud contacts in my UWP application. I know that I can access my Gmail Contacts through Google's People API and my Outlook contacts through Microsoft's Graph api OR Outook people api .

Does Apple provide an API (Rest or otherwise) that could be leveraged to fetch, update, add, delete contacts? If yes, is there a tutorial that walks through how to setup access iCloud api?

So, if you just want to fetch the contacts (and in JSON format) my previous answer is probably the way to go. However, as I wanted to be able to perform CRUD operations on Apple account, a better way is to use the CARDDAV protocol that icloud/apple supports.

  1. Get the principle user using basic authentication (the password used in this basic authentication would have to be generated as an app password here) by firing a PROPFIND request at https://contacts.icloud.com with the request content:

<propfind
xmlns="DAV:">
<prop>
    <current-user-principal/>
</prop>
</propfind> 

  1. The previous step will let you retrieve a principle which will be of the format /1437425399/principal/. Now, you can fire a PROPFIND query the home-set for this user at the link https://contacts.icloud.com/1437425399/principal with the following request content:

<propfind
    xmlns="DAV:"
    xmlns:c="urn:ietf:params:xml:ns:carddav">
    <prop>
        <c:addressbook-home-set/>
    </prop>
</propfind>

  1. From the previous request, you will get the link to the homeset in the format https://p48-contacts.icloud.com:443/1437425399/carddavhome/. You can query where the vcards for the user exist using the following PROPFIND request at the home-set link:

<propfind
xmlns="DAV:">
<prop>
    <resourcetype/>
</prop>
</propfind>

  1. You will receive the place where all the cards are placed (eg. /1437425399/carddavhome/card/). Using a addressbook query, now you can initiate a REPORT request at the endpoint received in the previous link:

<c:addressbook-query
    xmlns="DAV:"
    xmlns:c="urn:ietf:params:xml:ns:carddav">
    <prop>
    </prop>
</c:addressbook-query>

  1. This will give you all the cards in the tag. You can then fetch multiple cards using a addressbook-multiget REPORT request:

<c:addressbook-multiget
    xmlns="DAV:"
    xmlns:c="urn:ietf:params:xml:ns:carddav">
    <prop>
        <getetag />
        <c:address-data />
    </prop>
    <href>/1437425399/carddavhome/card/somecard.vcf</href>
    <href>/1437425399/carddavhome/card/anothercard.vcf</href>
</c:addressbook-multiget>

For updating, creating and deleting cards, you can fire PUT requests at "/1437425399/carddavhome/card/cardtobemanipulated.vcf" endpoint using the basic authentication that we discussed earlier after setting the Content-Type as "text/vcard" and sending the VCard in the content for update and create requests.