Dienstkonten auflisten
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Veranschaulicht das Auflisten von Dienstkonten.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","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)."]]