使用客户端库授予角色
了解如何以您喜爱的编程语言开始使用 Resource Manager API 中的 IAM 方法。
准备工作
创建 Trusted Cloud 项目
在此快速入门中,您需要一个新的 Trusted Cloud 项目。
-
Install the Google Cloud CLI.
-
配置 gcloud CLI 以使用您的联合身份。
如需了解详情,请参阅使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
Create or select a Trusted Cloud project.
-
Create a Trusted Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Trusted Cloud project you are creating. -
Select the Trusted Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Trusted Cloud project name.
-
-
Enable the Resource Manager API:
gcloud services enable cloudresourcemanager.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/resourcemanager.projectIamAdmin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account. For examples, see Represent workforce pool users in IAM policies.ROLE
: the IAM role that you grant to your user account.
- 初始化用于管理 Trusted Cloud项目的 Resource Manager 服务。
- 读取项目的允许政策。
- 通过将 Log Writer 角色 (
roles/logging.logWriter
) 授予您的 Google 账号来修改允许政策。 - 写入更新后的允许政策。
- 输出项目级层具有 Log Writer 角色 (
roles/logging.logWriter
) 的所有主账号。 - 撤消 Log Writer 角色。
your-project
:您的项目的 ID。your-member
:用户账号的电子邮件地址。例如principal://iam.googleapis.com/locations/global/workforcePools/my-pool/subject/my-user@example.com
。-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
- 了解 IAM 的工作原理。
- 详细了解如何授予、更改和撤消访问权限。
- 使用政策问题排查工具排查访问权限问题。
安装客户端库
C#
如需详细了解如何设置 C# 开发环境,请参阅 C# 开发环境设置指南。
install-package Google.Apis.Iam.v1 install-package Google.Apis.CloudResourceManager.v1
Go
go get golang.org/x/oauth2/google go get google.golang.org/api/cloudresourcemanager/v1
Java
如需详细了解如何设置 Java 开发环境,请参阅 Java 开发环境设置指南。
如果您使用的是 Maven,请将其添加到您的pom.xml
文件中。
Python
如需详细了解如何设置 Python 开发环境,请参阅 Python 开发环境设置指南。
pip install --upgrade google-api-python-client google-auth google-auth-httplib2
读取、修改和写入允许政策
本快速入门中的代码段会执行以下操作:
在运行代码段之前替换以下值:
C#
如需了解如何安装和使用 Resource Manager 客户端库,请参阅 Resource Manager 客户端库。 如需了解详情,请参阅 Resource Manager C# API 参考文档。
如需向 Resource Manager 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
在运行代码示例之前,请将 GOOGLE_CLOUD_UNIVERSE_DOMAIN
环境变量设置为 s3nsapis.fr
。
Go
如需了解如何安装和使用 Resource Manager 客户端库,请参阅 Resource Manager 客户端库。 如需了解详情,请参阅 Resource Manager Go API 参考文档。
如需向 Resource Manager 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
在运行代码示例之前,请将 GOOGLE_CLOUD_UNIVERSE_DOMAIN
环境变量设置为 s3nsapis.fr
。
Java
如需了解如何安装和使用 Resource Manager 客户端库,请参阅 Resource Manager 客户端库。 如需了解详情,请参阅 Resource Manager Java API 参考文档。
如需向 Resource Manager 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
在运行代码示例之前,请将 GOOGLE_CLOUD_UNIVERSE_DOMAIN
环境变量设置为 s3nsapis.fr
。
Python
如需了解如何安装和使用 Resource Manager 客户端库,请参阅 Resource Manager 客户端库。 如需了解详情,请参阅 Resource Manager Python API 参考文档。
如需向 Resource Manager 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
在运行代码示例之前,请将 GOOGLE_CLOUD_UNIVERSE_DOMAIN
环境变量设置为 s3nsapis.fr
。
恭喜!您使用了 Resource Manager API 中的 IAM 方法来修改项目的访问权限。
结果怎么样?
清理
后续步骤