使用 CardDAV 协议管理联系人
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 Google 的 CardDAV 协议查看和管理联系人。
联系人存储在用户的 Google 账号中;大多数 Google 服务都可以访问通讯录。您的客户端应用可以使用 CardDAV API 创建新联系人、修改或删除现有联系人,以及查询与特定条件匹配的联系人。
规格
完整规范未实现,但许多客户端(如
Apple iOSTM 通讯录
和 macOS 应该都能正常互操作。
对于每个相关规范,Google 的 CardDAV 支持如下所示:
Google 的 CardDAV 要求使用 OAuth 2.0
Google 的 CardDAV 接口需要 OAuth 2.0。如需了解如何使用 OAuth 2.0 访问 Google API,请参阅以下链接的文档:
连接到 Google 的 CardDAV 服务器
CardDAV 协议允许发现地址簿和联系人资源 URI。请勿对任何 URI 进行硬编码,因为它们随时都可能发生变化。
客户端应用必须使用 HTTPS,且 OAuth 2.0
身份验证必须使用
为用户的 Google 账号提供的值。除非请求通过 HTTPS 传入并使用 Google 账号的 OAuth 2.0 身份验证,并且您的应用已在 Play 管理中心注册,否则 CardDAV 服务器不会对请求进行身份验证。任何尝试通过 HTTP 使用基本身份验证或使用与 Google 账号不匹配的电子邮件地址/密码进行连接的行为都会导致 HTTP 401 Unauthorized
响应代码。
要使用 CardDAV,您的客户端程序必须先连接到规范化服务器
发现路径,方法是对以下内容执行 HTTP PROPFIND
:
https://www.googleapis.com/.well-known/carddav
重定向 (HTTP 301
) 到地址簿资源后,您的客户端程序便可以对其执行 PROPFIND
以发现 DAV:current-user-principal
、DAV:principal-URL
和 addressbook-home-set
属性。然后,您的客户端程序可以通过对 addressbook-home-set
执行 PROPFIND
并查找 addressbook
和 collection
资源来发现主要地址簿。此流程的完整说明
不在本文档的讨论范围之内。如需了解详情,请参阅 rfc6352。
HTTP 301
响应中通过 PROPFIND
返回的重定向路径
已知 URI 不得被永久缓存(根据
rfc6764)。设备应定期重试知名 URI 发现,以验证缓存的路径是否仍是最新的,并在路径发生变化时重新同步。Google 建议每 2-4 周进行一次。
资源
CardDAV 使用 REST 概念。客户端应用针对
由其 URI 指定此处指定了当前的 URI 结构,以帮助开发者理解下一部分中的概念。结构
更改,且不得硬编码。而是应根据 RFC 发现资源。
- 主账号
- https://www.googleapis.com/carddav/v1/principals/
userEmail
- 住宅集合
- https://www.googleapis.com/carddav/v1/principals/
userEmail
/lists
- 通讯簿
- https://www.googleapis.com/carddav/v1/principals/
userEmail
/lists/default
- 联系
- https://www.googleapis.com/carddav/v1/principals/
userEmail
/lists/default/contactId
以下是对支持的操作的一般说明。开发者
请在相关的 RFC 中查找详细信息。请求和响应通常采用 XML 编码。以下是客户端应用用于同步的主要操作:
- 使用 CTag
- 客户端程序会对通讯录资源使用
getctag
PROPFIND
请求,以确定服务器上的任何联系人是否发生了更改,从而确定是否需要同步。如果任何联系人发生变化,此属性的值一定会发生变化。客户端应用
应存储此值,并仅在初次同步时将其用作
在 sync-token
失效时进行回退。定期轮询
getctag
属性将导致节流。
- 使用 sync-token
- 客户端程序对地址使用
sync-token
PROPFIND
请求
Book 以获取表示其当前状态的 sync-token
。客户
应用必须存储此值并定期发出 sync-collection
REPORT
个请求用于确定自上次发出后发生的更改
sync-token
。颁发的令牌的有效期为 29 天,并且REPORT
响应将包含新的 sync-token
。
- 使用 ETag
- 客户端应用针对地址发出
getetag
PROPFIND
请求
图书资源(DEPTH
标题等于 DEPTH_1
)。通过维护
每个联系人的 ETag
值,客户端程序可以请求该值
的联系人,其 ETag
发生了更改。
- 检索联系人
- 客户端应用通过发出
addressbook-multiget
REPORT
请求。根据给定的联系人 URI 列表
报告会以 VCard 3.0 值返回所有请求的联系人。每个条目都包含联系人的 ETag
。
- 插入联系人
- 客户端应用发出
POST
请求,其中包含采用 VCard 3.0 格式的新联系人。响应将包含新联系人的 ID。
- 更新联系人
- 客户端应用发出
PUT
请求,其中包含采用 VCard 3.0 格式的更新版联系人。如果联系人已存在于地址簿中,系统会更新该联系人。
- 客户端应用应包含一个
If-Match
标头,其中包含联系人的当前已知 ETag
。然后,服务器会拒绝 PUT
请求(包含 HTTP 412
),前提是服务器上的当前 ETag
为
与客户端程序发送的 ETag
不同。这样,
乐观更新序列化。
- 删除联系人
- 客户端应用通过发出
DELETE
请求来删除联系人
与联系人 URI 进行比对。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eGoogle Contacts can be accessed and managed using the CardDAV protocol, enabling client applications to create, edit, delete, and query contacts.\u003c/p\u003e\n"],["\u003cp\u003eGoogle's CardDAV implementation requires OAuth 2.0 for authentication and HTTPS for secure connections.\u003c/p\u003e\n"],["\u003cp\u003eClient applications should discover resource URIs dynamically instead of hardcoding them, as they are subject to change.\u003c/p\u003e\n"],["\u003cp\u003eContact synchronization can be achieved using CTag, sync-token, or ETags to efficiently track and update changes.\u003c/p\u003e\n"],["\u003cp\u003eGoogle's CardDAV utilizes VCard 3.0 for encoding contact data.\u003c/p\u003e\n"]]],["Google's CardDAV protocol allows managing contacts stored in a Google Account. Client applications can create, edit, delete, and query contacts using the CardDAV API. Key actions include using `PROPFIND` for discovery and obtaining `sync-token` and `getctag` for synchronization. Retrieving contacts is done with `addressbook-multiget REPORT`, while inserting, updating, and deleting contacts utilize `POST`, `PUT` (with `If-Match`), and `DELETE` requests, respectively. Authentication requires HTTPS and OAuth 2.0, and VCard 3.0 is the contact encoding format.\n"],null,["# Manage contacts with the CardDAV protocol\n\nYou can view and manage your contacts using Google's CardDAV protocol.\n\nContacts are stored in the user's Google Account; most Google services have\naccess to the contact list. Your client application can use the CardDAV API to\ncreate new contacts, edit or delete existing contacts, and query for contacts\nthat match particular criteria.\n\nSpecifications\n--------------\n\nThe full specification is not implemented, but many clients such as\n[Apple iOS™ Contacts](https://support.google.com/contacts/answer/2753077?co=GENIE.Platform%3DiOS)\nand macOS should interoperate correctly.\n\nFor each relevant specification, Google's CardDAV support is as follows:\n\n- [rfc2518: HTTP Extensions for Distributed Authoring (WebDAV)](https://tools.ietf.org/html/rfc2518)\n - Supports the HTTP methods `GET`, `PUT`, `DELETE`, `OPTIONS`, and `PROPFIND`.\n - Does *not* support the HTTP methods `LOCK`, `UNLOCK`, `COPY`, `MOVE`, or `MKCOL`.\n - Does *not* support arbitrary (user-defined) WebDAV properties.\n - Does *not* support WebDAV Access Control (rfc3744).\n- [rfc5995: Using POST to Add Members to WebDAV Collections](https://tools.ietf.org/html/rfc5995)\n - Supports creating new contacts without specifying an ID.\n- [rfc6352: CardDAV: vCard Extensions to Web Distributed Authoring and\n Versioning (WebDAV)](https://tools.ietf.org/html/rfc6352)\n - Supports the HTTP method `REPORT`, but not all defined reports are implemented.\n - Supports providing a principal collection and a contacts collection.\n- [rfc6578: Collection Synchronization for WebDAV](https://tools.ietf.org/html/rfc6578)\n - Client applications must switch to this mode of operation after the initial sync.\n- [rfc6749: The OAuth 2.0 Authorization Framework](https://tools.ietf.org/html/rfc6749) and [rfc6750: The OAuth 2.0 Authorization Framework: Bearer Token Usage](https://tools.ietf.org/html/rfc6750)\n - Supports authenticating CardDAV client programs using OAuth 2.0 HTTP Authentication. Google does not support any other authentication method. For security of contact data, we require CardDAV connections to use [HTTPS](https://en.wikipedia.org/wiki/HTTPS).\n- [rfc6764: Locating Services for Calendaring Extensions to WebDAV (CalDAV) and vCard Extensions to WebDAV (CardDAV)](https://tools.ietf.org/html/rfc6764)\n - Bootstrapping of CardDAV URLs must take place according to section 6 of rfc6764.\n- Supports [caldav-ctag-02: Calendar Collection Entity Tag (CTag) in CalDAV](https://github.com/apple/ccs-calendarserver/blob/master/doc/Extensions/caldav-ctag.txt), which is shared between the CardDAV and CalDAV specifications. The contacts `ctag` is like a resource `ETag`; it changes when anything in the contact address book has changed. This allows the client program to quickly determine that it does not need to synchronize any changed contacts.\n- Google uses VCard 3.0 as the contact encoding format. See: [rfc6350: VCard 3.0](https://tools.ietf.org/html/rfc6350).\n\nGoogle's CardDAV requires OAuth 2.0\n-----------------------------------\n\nGoogle's CardDAV interface requires OAuth 2.0. Refer to the linked\ndocumentation below for information on using OAuth 2.0 to access Google APIs:\n\n- [Using OAuth 2.0 to Access Google APIs](https://developers.google.com/identity/protocols/oauth2)\n- [Using OAuth 2.0 for Installed Applications](https://developers.google.com/identity/protocols/oauth2/native-app)\n\nConnecting to Google's CardDAV server\n-------------------------------------\n\nThe CardDAV protocol allows discovery of the address book and contact resources\nURIs. You must not hardcode any URI as they could change at any time.\n\nClient applications must use HTTPS, and `OAuth 2.0` authentication must be\nprovided for the user's Google account. The CardDAV server will not\nauthenticate a request unless it arrives over HTTPS with OAuth 2.0\nauthentication of a Google account, and your application is registered on\nDevConsole. Any attempt to connect over HTTP with Basic authentication or with\nan email/password that doesn't match a Google account results in an HTTP\n`401 Unauthorized` response code.\n\nTo use CardDAV, your client program must initially connect to the canonical\ndiscovery path by performing an HTTP `PROPFIND` on: \n\n https://www.googleapis.com/.well-known/carddav\n\nOnce redirected (`HTTP 301`) to an Address Book Resource, your client program\ncan then perform a `PROPFIND` on it to discover the\n`DAV:current-user-principal`, `DAV:principal-URL`, and `addressbook-home-set`\nproperties. Your client program can then discover the principal address book by\nperforming a `PROPFIND` on the `addressbook-home-set` and looking for the\n`addressbook` and `collection` resources. A full description of this process\nis beyond the scope of this document. See\n[rfc6352](https://tools.ietf.org/html/rfc6352) for more details.\n\nThe redirect path returned in the `HTTP 301` response through a `PROPFIND` on\nthe well-known URI must **not** be permanently cached (as per\n[rfc6764](https://tools.ietf.org/html/rfc6764)). Devices should retry well-known\nURI discovery periodically to verify if the cached path is still up to date and\nresync if the path ever changes. Google recommends a rate of every 2-4 weeks.\n\nResources\n---------\n\nCardDAV uses REST concepts. Client applications act on resources that are\ndesignated by their URIs. The current URI structure is specified here to help\ndevelopers understand the concepts in the following section. The structure may\nchange and must not be hardcoded. Rather, the resources should be discovered\naccording to the RFC.\n\n1. **Principal**\n - https://www.googleapis.com/carddav/v1/principals/\u003cvar class=\"apiparam\" translate=\"no\"\u003euserEmail\u003c/var\u003e\n2. **Home Set**\n - https://www.googleapis.com/carddav/v1/principals/\u003cvar class=\"apiparam\" translate=\"no\"\u003euserEmail\u003c/var\u003e/lists\n3. **Address Book**\n - https://www.googleapis.com/carddav/v1/principals/\u003cvar class=\"apiparam\" translate=\"no\"\u003euserEmail\u003c/var\u003e/lists/default\n4. **Contact**\n - https://www.googleapis.com/carddav/v1/principals/\u003cvar class=\"apiparam\" translate=\"no\"\u003euserEmail\u003c/var\u003e/lists/default/\u003cvar class=\"apiparam\" translate=\"no\"\u003econtactId\u003c/var\u003e\n\nSynchronizing Contacts\n----------------------\n\nThe following is a general description of the operations supported. Developers\nshould look for the details in the relevant RFC. Requests and responses are\nmostly encoded in XML. These are the main operations used by client\napplications for synchronization:\n\n- **Using CTag**\n - Client programs use the `getctag` `PROPFIND` request on the Address Book resource to determine if any contact has changed on the server and therefore whether a synchronization is needed. The value of this property is guaranteed to change if any contact changes. Client applications should store this value and use it only on the initial sync and as a fallback when a `sync-token` is invalidated. Periodically polling for the `getctag` property will result in throttling.\n- **Using sync-token**\n - Client programs use the `sync-token` `PROPFIND` request on the Address Book to obtain the `sync-token` representing its current state. Client applications must store this value and issue periodic `sync-collection` `REPORT` requests to determine changes since the last issued `sync-token`. Issued tokens are valid for 29 days, and the `REPORT` response will contain a new `sync-token`.\n- **Using ETags**\n - Client applications issue a `getetag` `PROPFIND` request on the Address Book resource (with `DEPTH` header equal to `DEPTH_1`). By maintaining the `ETag` value of each contact, a client program can request the value of contacts that had their `ETag` changed.\n- **Retrieving contacts**\n - Client applications retrieve contacts by issuing an `addressbook-multiget` `REPORT` request. Given a list of contact URIs, the report returns all the requested contacts as VCard 3.0 values. Each entry includes an `ETag` for the contact.\n- **Inserting a contact**\n - Client applications issue a `POST` request with the new contact in VCard 3.0 format. The response will contain the ID of the new contact.\n- **Updating a contact**\n - Client applications issue a `PUT` request with the updated contact in VCard 3.0 format. The contact is updated if the contact already exists in the address book.\n - Client applications should include an `If-Match` header with the contact's currently known `ETag`. The server will then reject the `PUT` request (with `HTTP 412`) if the current `ETag` on the server is different from the `ETag` sent by the client program. This allows for optimistic serialization of updates.\n- **Deleting a contact**\n - Client applications delete a contact by issuing a `DELETE` request against the contact URI."]]