列出服务账号
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示如何列出服务账号。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 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,["# List service accounts\n\nDemonstrates listing service accounts.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [List and edit service accounts](/iam/docs/service-accounts-list-edit)\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& project_id) {\n iam::IAMClient client(iam::MakeIAMConnection());\n int count = 0;\n for (auto& sa : client.ListServiceAccounts(\"projects/\" + project_id)) {\n if (!sa) throw std::move(sa).status();\n std::cout \u003c\u003c \"ServiceAccount successfully retrieved: \" \u003c\u003c sa-\u003ename()\n \u003c\u003c \"\\n\";\n ++count;\n }\n if (count == 0) {\n std::cout \u003c\u003c \"No service accounts found in project: \" \u003c\u003c project_id\n \u003c\u003c \"\\n\";\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 System.Collections.Generic;\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 ServiceAccounts\n {\n public static IList\u003cServiceAccount\u003e ListServiceAccounts(string projectId)\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 var response = service.Projects.ServiceAccounts.List(\n \"projects/\" + projectId).Execute();\n foreach (ServiceAccount account in response.Accounts)\n {\n Console.WriteLine(\"Name: \" + account.Name);\n Console.WriteLine(\"Display Name: \" + account.DisplayName);\n Console.WriteLine(\"Email: \" + account.Email);\n Console.WriteLine();\n }\n return response.Accounts;\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 // listServiceAccounts lists a project's service accounts.\n func listServiceAccounts(w io.Writer, projectID string) ([]*iam.ServiceAccount, error) {\n \tctx := context.Background()\n \tservice, err := iam.NewService(ctx)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"iam.NewService: %w\", err)\n \t}\n\n \tresponse, err := service.Projects.ServiceAccounts.List(\"projects/\" + projectID).Do()\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"Projects.ServiceAccounts.List: %w\", err)\n \t}\n \tfor _, account := range response.Accounts {\n \t\tfmt.Fprintf(w, \"Listing service account: %v\\n\", account.Name)\n \t}\n \treturn response.Accounts, 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.ServiceAccount.html;\n import java.io.IOException;\n\n public class ListServiceAccounts {\n\n public static void main(String[] args) throws IOException {\n // TODO(Developer): Replace the below variables before running.\n String projectId = \"your-project-id\";\n\n listServiceAccounts(projectId);\n }\n\n // Lists all service accounts for the current project.\n public static https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.ListServiceAccountsPagedResponse.html listServiceAccounts(String projectId)\n 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 https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.ListServiceAccountsPagedResponse.html response =\n iamClient.listServiceAccounts(String.format(\"projects/%s\", projectId));\n\n for (https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.ServiceAccount.html account : response.iterateAll()) {\n System.out.println(\"Name: \" + account.getName());\n System.out.println(\"Display name: \" + account.getDisplayName());\n System.out.println(\"Email: \" + account.getEmail() + \"\\n\");\n }\n\n return response;\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 typing import List\n\n from google.cloud import iam_admin_v1\n from google.cloud.iam_admin_v1 import types\n\n\n def list_service_accounts(project_id: str) -\u003e List[iam_admin_v1.ServiceAccount]:\n \"\"\"Get list of project service accounts.\n\n project_id: ID or number of the Google Cloud project you want to use.\n\n returns a list of iam_admin_v1.ServiceAccount\n \"\"\"\n\n iam_admin_client = iam_admin_v1.IAMClient()\n request = types.ListServiceAccountsRequest()\n request.name = f\"projects/{project_id}\"\n\n accounts = iam_admin_client.list_service_accounts(request=request)\n return accounts.accounts\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)."]]