啟用服務帳戶金鑰
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
示範如何啟用已停用的 IAM 服務帳戶金鑰。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。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"]],[],[[["\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)."]]