Enable a service account key
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates enabling a disabled IAM service account key.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to enable a disabled service account key using the IAM client library in Java.\u003c/p\u003e\n"],["\u003cp\u003eThe code requires the project ID, service account name, and service account key name to enable the service account key.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eenableServiceAccountKey\u003c/code\u003e function utilizes the IAM client to enable the specified service account key, referencing the documentation for further details.\u003c/p\u003e\n"],["\u003cp\u003eThe service account key must be properly formatted to reference the correct key that will be enabled, using the project ID, service account email, and service account key.\u003c/p\u003e\n"],["\u003cp\u003eTo access other Google Cloud code examples, one can use the provided Google Cloud sample browser link to search and filter for other products.\u003c/p\u003e\n"]]],[],null,["# Enable a service account key\n\nDemonstrates enabling a disabled IAM service account key.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Disable and enable service account keys](/iam/docs/keys-disable-enable)\n\nCode sample\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 java.io.IOException;\n\n\n public class EnableServiceAccountKey {\n\n public static void main(String[] args) throws IOException {\n // TODO(Developer): Replace the below variables before running.\n String projectId = \"gcloud-project-id\";\n String serviceAccountName = \"service-account-name\";\n String serviceAccountKeyName = \"service-account-key-name\";\n\n enableServiceAccountKey(projectId, serviceAccountName, serviceAccountKeyName);\n }\n\n // Enables a service account key.\n public static void enableServiceAccountKey(String projectId,\n String accountName,\n String key) throws IOException {\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 enable the key.\n // See, https://cloud.google.com/iam/docs/creating-managing-service-account-keys#enabling\n String email = String.format(\"%s@%s.iam.gserviceaccount.com\", accountName, projectId);\n String name = String.format(\"projects/%s/serviceAccounts/%s/keys/%s\", projectId, email, key);\n\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 iamClient.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html#com_google_cloud_iam_admin_v1_IAMClient_enableServiceAccountKey_com_google_iam_admin_v1_EnableServiceAccountKeyRequest_(name);\n\n System.out.println(\"Enabled service account key: \" + name);\n }\n }\n }\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)."]]