停用服务账号密钥
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示如何停用 IAM 服务账号密钥。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 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,["# Disable a service account key\n\nDemonstrates disabling an 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 DisableServiceAccountKey {\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 disableServiceAccountKey(projectId, serviceAccountName, serviceAccountKeyName);\n }\n\n // Disables a service account key.\n public static void disableServiceAccountKey(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 disable the key.\n // See, https://cloud.google.com/iam/docs/creating-managing-service-account-keys#disabling\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_disableServiceAccountKey_com_google_iam_admin_v1_DisableServiceAccountKeyRequest_(name);\n\n System.out.println(\"Disabled 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)."]]