删除服务账号密钥
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示如何删除服务账号密钥。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Delete a service account key\n\nDemonstrates deleting a service account key.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and delete service account keys](/iam/docs/keys-create-delete)\n\nCode sample\n-----------\n\n### C++\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM C++ API\nreference documentation](/cpp/docs/reference/iam/latest).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n namespace iam = ::google::cloud::iam_admin_v1;\n [](std::string const& name) {\n iam::IAMClient client(iam::MakeIAMConnection());\n auto response = client.DeleteServiceAccountKey(name);\n if (!response.ok()) throw std::runtime_error(response.message());\n std::cout \u003c\u003c \"ServiceAccountKey successfully deleted.\\n\";\n }\n\n### C#\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM C# API\nreference documentation](https://developers.google.com/api-client-library/dotnet/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n using System;\n using https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.html;\n using Google.Apis.Iam.v1;\n using Google.Apis.Iam.v1.Data;\n\n public partial class ServiceAccountKeys\n {\n public static void DeleteKey(string fullKeyName)\n {\n var credential = https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential.html.https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential.html#Google_Apis_Auth_OAuth2_GoogleCredential_GetApplicationDefault()\n .https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential.html#Google_Apis_Auth_OAuth2_GoogleCredential_CreateScoped_System_Collections_Generic_IEnumerable_System_String__(IamService.Scope.CloudPlatform);\n var service = new IamService(new IamService.Initializer\n {\n HttpClientInitializer = credential\n });\n\n service.Projects.ServiceAccounts.Keys.Delete(fullKeyName).Execute();\n Console.WriteLine(\"Deleted key: \" + fullKeyName);\n }\n }\n\n### Go\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Go API\nreference documentation](https://godoc.org/google.golang.org/genproto/googleapis/iam/admin/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tiam \"google.golang.org/api/iam/v1\"\n )\n\n // deleteKey deletes a service account key.\n func deleteKey(w io.Writer, fullKeyName string) error {\n \tctx := context.Background()\n \tservice, err := iam.NewService(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"iam.NewService: %w\", err)\n \t}\n\n \t_, err = service.Projects.ServiceAccounts.Keys.Delete(fullKeyName).Do()\n \tif err != nil {\n \t\treturn fmt.Errorf(\"Projects.ServiceAccounts.Keys.Delete: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Deleted key: %v\", fullKeyName)\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Java API\nreference documentation](https://developers.google.com/api-client-library/java/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html;\n import com.google.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.DeleteServiceAccountKeyRequest.html;\n import com.google.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.KeyName.html;\n import java.io.IOException;\n\n public class DeleteServiceAccountKey {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace the variables before running the sample.\n String projectId = \"your-project-id\";\n String serviceAccountName = \"my-service-account-name\";\n String serviceAccountKeyId = \"service-account-key-id\";\n\n deleteKey(projectId, serviceAccountName, serviceAccountKeyId);\n }\n\n // Deletes a service account key.\n public static void deleteKey(String projectId, String accountName,\n String serviceAccountKeyId) throws IOException {\n //Initialize client that will be used to send requests.\n //This client only needs to be created once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html iamClient = https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html.create()) {\n\n //Construct the service account email.\n //You can modify the \".iam.gserviceaccount.com\" to match the service account name in which\n //you want to delete the key.\n //See, https://cloud.google.com/iam/docs/creating-managing-service-account-keys#deleting\n\n String accountEmail = String.format(\"%s@%s.iam.gserviceaccount.com\", accountName, projectId);\n\n String name = https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.KeyName.html.of(projectId, accountEmail, serviceAccountKeyId).toString();\n\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.DeleteServiceAccountKeyRequest.html request = https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.DeleteServiceAccountKeyRequest.html.newBuilder()\n .setName(name)\n .build();\n\n // Then you can delete the key\n iamClient.deleteServiceAccountKey(request);\n\n System.out.println(\"Deleted key: \" + serviceAccountKeyId);\n }\n }\n }\n\n### Python\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Python API\nreference documentation](https://developers.google.com/api-client-library/python/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import iam_admin_v1\n from google.cloud.iam_admin_v1 import https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.html\n\n\n def delete_key(project_id: str, account: str, key_id: str) -\u003e None:\n \"\"\"Deletes a key for a service account.\n\n project_id: ID or number of the Google Cloud project you want to use.\n account: ID or email which is unique identifier of the service account.\n key_id: unique ID of the key.\n \"\"\"\n\n iam_admin_client = iam_admin_v1.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html()\n request = https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.html.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.DeleteServiceAccountKeyRequest.html()\n request.name = f\"projects/{project_id}/serviceAccounts/{account}/keys/{key_id}\"\n\n iam_admin_client.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html#google_cloud_iam_admin_v1_services_iam_IAMClient_delete_service_account_key(request=request)\n print(f\"Deleted key: {key_id}\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=iam)."]]