{"meta":{"title":"GPG 密钥的 REST API 终结点","intro":"使用 REST API 管理经过身份验证的用户的 GPG 密钥。","product":"REST API","breadcrumbs":[{"href":"/zh/rest","title":"REST API"},{"href":"/zh/rest/users","title":"用户"},{"href":"/zh/rest/users/gpg-keys","title":"GPG 密钥"}],"documentType":"article"},"body":"# GPG 密钥的 REST API 终结点\n\n使用 REST API 管理经过身份验证的用户的 GPG 密钥。\n\n## 关于用户 GPG 密钥管理\n\n在 `public_key` 响应字段中返回的数据不是 GPG 格式的密钥。 当用户上传 GPG 密钥时，将对密钥进行剖析，然后提取并存储加密公钥。 此加密密钥是此类别中的终结点将返回的内容。 此密钥不适合在 GPG 等程序中直接使用。\n\n如果请求 URL 不包含 `{username}` 参数，则响应将针对已登录的用户（并且必须使用请求传递 [身份验证信息](/zh/rest/authentication/authenticating-to-the-rest-api)）。 在通过作用域为 `user` 的 OAuth 进行身份验证时，将包含其他专用信息，例如用户是否启用双重身份验证。\n\n> \\[!NOTE]\n> Most endpoints use `Authorization: Bearer <YOUR-TOKEN>` and `Accept: application/vnd.github+json` headers, plus `X-GitHub-Api-Version: 2026-03-10`. Curl examples below omit these standard headers for brevity.\n\n## List GPG keys for the authenticated user\n\n```\nGET /user/gpg_keys\n```\n\nLists the current user's GPG keys.\nOAuth app tokens and personal access tokens (classic) need the read:gpg\\_key scope to use this endpoint.\n\n### Parameters\n\n#### Headers\n\n* **`accept`** (string)\n  Setting to `application/vnd.github+json` is recommended.\n\n#### Path and query parameters\n\n* **`per_page`** (integer)\n  The number of results per page (max 100). For more information, see \"Using pagination in the REST API.\"\n  Default: `30`\n\n* **`page`** (integer)\n  The page number of the results to fetch. For more information, see \"Using pagination in the REST API.\"\n  Default: `1`\n\n### HTTP response status codes\n\n* **200** - OK\n\n* **304** - Not modified\n\n* **401** - Requires authentication\n\n* **403** - Forbidden\n\n* **404** - Resource not found\n\n### Code examples\n\n#### Example\n\n**Request:**\n\n```curl\ncurl -L \\\n  -X GET \\\n  https://api.github.com/user/gpg_keys\n```\n\n**Response schema (Status: 200):**\n\nArray of `GPG Key`:\n\n* `id`: required, integer, format: int64\n* `name`: string or null\n* `primary_key_id`: required, integer or null\n* `key_id`: required, string\n* `public_key`: required, string\n* `emails`: required, array of objects:\n  * `email`: string\n  * `verified`: boolean\n* `subkeys`: required, array of objects:\n  * `id`: integer, format: int64\n  * `primary_key_id`: integer\n  * `key_id`: string\n  * `public_key`: string\n  * `emails`: array of objects:\n    * `email`: string\n    * `verified`: boolean\n  * `subkeys`: array of object\n  * `can_sign`: boolean\n  * `can_encrypt_comms`: boolean\n  * `can_encrypt_storage`: boolean\n  * `can_certify`: boolean\n  * `created_at`: string\n  * `expires_at`: string or null\n  * `raw_key`: string or null\n  * `revoked`: boolean\n* `can_sign`: required, boolean\n* `can_encrypt_comms`: required, boolean\n* `can_encrypt_storage`: required, boolean\n* `can_certify`: required, boolean\n* `created_at`: required, string, format: date-time\n* `expires_at`: required, string or null, format: date-time\n* `revoked`: required, boolean\n* `raw_key`: required, string or null\n\n## Create a GPG key for the authenticated user\n\n```\nPOST /user/gpg_keys\n```\n\nAdds a GPG key to the authenticated user's GitHub account.\nOAuth app tokens and personal access tokens (classic) need the write:gpg\\_key scope to use this endpoint.\n\n### Parameters\n\n#### Headers\n\n* **`accept`** (string)\n  Setting to `application/vnd.github+json` is recommended.\n\n#### Body parameters\n\n* **`name`** (string)\n  A descriptive name for the new key.\n\n* **`armored_public_key`** (string) (required)\n  A GPG key in ASCII-armored format.\n\n### HTTP response status codes\n\n* **201** - Created\n\n* **304** - Not modified\n\n* **401** - Requires authentication\n\n* **403** - Forbidden\n\n* **404** - Resource not found\n\n* **422** - Validation failed, or the endpoint has been spammed.\n\n### Code examples\n\n#### Example\n\n**Request:**\n\n```curl\ncurl -L \\\n  -X POST \\\n  https://api.github.com/user/gpg_keys \\\n  -d '{\n  \"name\": \"Octocat's GPG Key\",\n  \"armored_public_key\": \"-----BEGIN PGP PUBLIC KEY BLOCK-----\\nVersion: GnuPG v1\\n\\nmQINBFnZ2ZIBEADQ2Z7Z7\\n-----END PGP PUBLIC KEY BLOCK-----\"\n}'\n```\n\n**Response schema (Status: 201):**\n\n* `id`: required, integer, format: int64\n* `name`: string or null\n* `primary_key_id`: required, integer or null\n* `key_id`: required, string\n* `public_key`: required, string\n* `emails`: required, array of objects:\n  * `email`: string\n  * `verified`: boolean\n* `subkeys`: required, array of objects:\n  * `id`: integer, format: int64\n  * `primary_key_id`: integer\n  * `key_id`: string\n  * `public_key`: string\n  * `emails`: array of objects:\n    * `email`: string\n    * `verified`: boolean\n  * `subkeys`: array of object\n  * `can_sign`: boolean\n  * `can_encrypt_comms`: boolean\n  * `can_encrypt_storage`: boolean\n  * `can_certify`: boolean\n  * `created_at`: string\n  * `expires_at`: string or null\n  * `raw_key`: string or null\n  * `revoked`: boolean\n* `can_sign`: required, boolean\n* `can_encrypt_comms`: required, boolean\n* `can_encrypt_storage`: required, boolean\n* `can_certify`: required, boolean\n* `created_at`: required, string, format: date-time\n* `expires_at`: required, string or null, format: date-time\n* `revoked`: required, boolean\n* `raw_key`: required, string or null\n\n## Get a GPG key for the authenticated user\n\n```\nGET /user/gpg_keys/{gpg_key_id}\n```\n\nView extended details for a single GPG key.\nOAuth app tokens and personal access tokens (classic) need the read:gpg\\_key scope to use this endpoint.\n\n### Parameters\n\n#### Headers\n\n* **`accept`** (string)\n  Setting to `application/vnd.github+json` is recommended.\n\n#### Path and query parameters\n\n* **`gpg_key_id`** (integer) (required)\n  The unique identifier of the GPG key.\n\n### HTTP response status codes\n\n* **200** - OK\n\n* **304** - Not modified\n\n* **401** - Requires authentication\n\n* **403** - Forbidden\n\n* **404** - Resource not found\n\n### Code examples\n\n#### Example\n\n**Request:**\n\n```curl\ncurl -L \\\n  -X GET \\\n  https://api.github.com/user/gpg_keys/GPG_KEY_ID\n```\n\n**Response schema (Status: 200):**\n\nSame response schema as [Create a GPG key for the authenticated user](#create-a-gpg-key-for-the-authenticated-user).\n\n## Delete a GPG key for the authenticated user\n\n```\nDELETE /user/gpg_keys/{gpg_key_id}\n```\n\nRemoves a GPG key from the authenticated user's GitHub account.\nOAuth app tokens and personal access tokens (classic) need the admin:gpg\\_key scope to use this endpoint.\n\n### Parameters\n\n#### Headers\n\n* **`accept`** (string)\n  Setting to `application/vnd.github+json` is recommended.\n\n#### Path and query parameters\n\n* **`gpg_key_id`** (integer) (required)\n  The unique identifier of the GPG key.\n\n### HTTP response status codes\n\n* **204** - No Content\n\n* **304** - Not modified\n\n* **401** - Requires authentication\n\n* **403** - Forbidden\n\n* **404** - Resource not found\n\n* **422** - Validation failed, or the endpoint has been spammed.\n\n### Code examples\n\n#### Example\n\n**Request:**\n\n```curl\ncurl -L \\\n  -X DELETE \\\n  https://api.github.com/user/gpg_keys/GPG_KEY_ID\n```\n\n**Response schema (Status: 204):**\n\n## List GPG keys for a user\n\n```\nGET /users/{username}/gpg_keys\n```\n\nLists the GPG keys for a user. This information is accessible by anyone.\n\n### Parameters\n\n#### Headers\n\n* **`accept`** (string)\n  Setting to `application/vnd.github+json` is recommended.\n\n#### Path and query parameters\n\n* **`username`** (string) (required)\n  The handle for the GitHub user account.\n\n* **`per_page`** (integer)\n  The number of results per page (max 100). For more information, see \"Using pagination in the REST API.\"\n  Default: `30`\n\n* **`page`** (integer)\n  The page number of the results to fetch. For more information, see \"Using pagination in the REST API.\"\n  Default: `1`\n\n### HTTP response status codes\n\n* **200** - OK\n\n### Code examples\n\n#### Example\n\n**Request:**\n\n```curl\ncurl -L \\\n  -X GET \\\n  https://api.github.com/users/USERNAME/gpg_keys\n```\n\n**Response schema (Status: 200):**\n\nSame response schema as [List GPG keys for the authenticated user](#list-gpg-keys-for-the-authenticated-user)."}