Get a role
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates retrieving a role.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples demonstrating how to retrieve a role using the IAM API in various programming languages, including C++, C#, Go, Java, and Python.\u003c/p\u003e\n"],["\u003cp\u003eEach code example illustrates the process of fetching a role's details using the \u003ccode\u003eGetRole\u003c/code\u003e method specific to the respective language's IAM client library.\u003c/p\u003e\n"],["\u003cp\u003eThe examples also highlight the importance of setting up Application Default Credentials (ADC) for authentication to the IAM API in a local development environment.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation contains links to detailed information regarding creating and managing custom roles, using client libraries, and the API reference for each language.\u003c/p\u003e\n"],["\u003cp\u003eUsers can find additional code samples for other Google Cloud products via the Google Cloud sample browser link that's provided at the end of the document.\u003c/p\u003e\n"]]],[],null,["# Get a role\n\nDemonstrates retrieving a role.\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& name) {\n iam::IAMClient client(iam::MakeIAMConnection());\n google::iam::admin::v1::GetRoleRequest request;\n request.set_name(name);\n auto response = client.GetRole(request);\n if (!response) throw std::move(response).status();\n std::cout \u003c\u003c \"Role successfully retrieved: \" \u003c\u003c response-\u003eDebugString()\n \u003c\u003c \"\\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 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 Role GetRole(string name)\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 role = service.Roles.Get(name).Execute();\n Console.WriteLine(role.Name);\n Console.WriteLine(String.Join(\", \", role.IncludedPermissions));\n return role;\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 // getRole gets role metadata.\n func getRole(w io.Writer, name 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 \trole, err := service.Roles.Get(name).Do()\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"Roles.Get: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Got role: %v\\n\", role.Name)\n \tfor _, permission := range role.IncludedPermissions {\n \t\tfmt.Fprintf(w, \"Got permission: %v\\n\", permission)\n \t}\n \treturn role, 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.GetRoleRequest.html;\n import com.google.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html;\n import java.io.IOException;\n\n /** Get role metadata. Specifically, printing out role permissions. */\n public class GetRole {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace the variable before running the sample.\n String roleId = \"a unique identifier (e.g. testViewer)\";\n\n getRole(roleId);\n }\n\n public static void getRole(String roleId) throws IOException {\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.GetRoleRequest.html getRoleRequest = https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.GetRoleRequest.html.newBuilder().setName(roleId).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.iam.admin.v1.Role.html role = iamClient.getRole(getRoleRequest);\n role.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html#com_google_iam_admin_v1_Role_getIncludedPermissionsList__().forEach(permission -\u003e System.out.println(permission));\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.api_core.exceptions import NotFound\n from google.cloud.iam_admin_v1 import https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.GetRoleRequest.html, 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.Role.html\n\n\n def get_role(project_id: str, role_id: str) -\u003e Role:\n client = IAMClient()\n name = f\"projects/{project_id}/roles/{role_id}\"\n request = GetRoleRequest(name=name)\n try:\n role = 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_get_role(request)\n print(f\"Retrieved role: {role_id}: {role}\")\n return role\n except NotFound as exc:\n raise NotFound(f\"Role with id [{role_id}] not found, take some actions\") from exc\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)."]]