Elimina una chiave del service account
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Mostra l'eliminazione di una chiave del account di servizio.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]