列出角色
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示了商家信息角色。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 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"]],[],[[["\u003cp\u003eThis document provides code samples in C++, C#, Go, Java, and Python demonstrating how to list roles within a project using the IAM client libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples utilize the \u003ccode\u003eListRoles\u003c/code\u003e method from the IAM API to retrieve a list of roles associated with a specified project.\u003c/p\u003e\n"],["\u003cp\u003eEach code example includes instructions for authenticating to IAM using Application Default Credentials.\u003c/p\u003e\n"],["\u003cp\u003eLinks are provided to documentation for creating and managing custom roles, client libraries, and the API reference for each language.\u003c/p\u003e\n"],["\u003cp\u003eInstructions are provided on how to find further samples using the Google Cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# List roles\n\nDemonstrates listing roles.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and manage custom roles](/iam/docs/creating-custom-roles)\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) {\n iam::IAMClient client(iam::MakeIAMConnection());\n int count = 0;\n google::iam::admin::v1::ListRolesRequest request;\n request.set_parent(project);\n for (auto& role : client.ListRoles(request)) {\n if (!role) throw std::move(role).status();\n std::cout \u003c\u003c \"Roles successfully retrieved: \" \u003c\u003c role-\u003ename() \u003c\u003c \"\\n\";\n ++count;\n }\n if (count == 0) {\n std::cout \u003c\u003c \"No roles found in project: \" \u003c\u003c project \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 CustomRoles\n {\n public static IList\u003cRole\u003e ListRoles(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.Roles.List(\"projects/\" + projectId)\n .Execute();\n foreach (var role in response.Roles)\n {\n Console.WriteLine(role.Name);\n }\n return response.Roles;\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 // listRoles lists a project's roles.\n func listRoles(w io.Writer, projectID string) ([]*iam.Role, 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.Roles.List(\"projects/\" + projectID).Do()\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"Projects.Roles.List: %w\", err)\n \t}\n \tfor _, role := range response.Roles {\n \t\tfmt.Fprintf(w, \"Listing role: %v\\n\", role.Name)\n \t}\n \treturn response.Roles, 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.cloud.iam.admin.v1.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.ListRolesPagedResponse.html;\n import com.google.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.ListRolesRequest.html;\n import java.io.IOException;\n\n /** List roles in a project. */\n public class ListRoles {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace the variable before running the sample.\n String projectId = \"your-project-id\";\n\n listRoles(projectId);\n }\n\n public static void listRoles(String projectId) throws IOException {\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.ListRolesRequest.html listRolesRequest =\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.ListRolesRequest.html.newBuilder().setParent(\"projects/\" + projectId).build();\n\n // Initialize client for sending requests. This client only needs to be created\n // 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.ListRolesPagedResponse.html listRolesResponse = iamClient.listRoles(listRolesRequest);\n listRolesResponse.iterateAll().forEach(role -\u003e System.out.println(role));\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 google.cloud.iam_admin_v1 import https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html, https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.ListRolesRequest.html, https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.RoleView.html\n from google.cloud.iam_admin_v1.services.iam.pagers import https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.pagers.ListRolesPager.html\n\n\n def list_roles(\n project_id: str, show_deleted: bool = True, role_view: RoleView = https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.RoleView.html.BASIC\n ) -\u003e ListRolesPager:\n \"\"\"Lists IAM roles in a GCP project.\n\n Args:\n project_id: GCP project ID\n show_deleted: Whether to include deleted roles in the results\n role_view: Level of detail for the returned roles (e.g., BASIC or FULL)\n\n Returns: A pager for traversing through the roles\n \"\"\"\n\n client = IAMClient()\n parent = f\"projects/{project_id}\"\n request = ListRolesRequest(parent=parent, show_deleted=show_deleted, view=role_view)\n roles = client.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html#google_cloud_iam_admin_v1_services_iam_IAMClient_list_roles(request)\n for page in roles.pages:\n for role in page.roles:\n print(role)\n print(\"Listed all iam roles\")\n return roles\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)."]]