使用用戶端程式庫授予角色
瞭解如何以您慣用的程式語言,開始使用 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.
- 初始化 Resource Manager 服務,該服務可管理專案。 Trusted Cloud
- 讀取專案的允許政策。
- 修改允許政策,將記錄檔寫入者角色 (
roles/logging.logWriter
) 授予您的 Google 帳戶。 - 寫入更新後的允許政策。
- 顯示具備專案層級記錄檔寫入者角色 (
roles/logging.logWriter
) 的所有主體。 - 撤銷記錄檔寫入者角色。
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 C# API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為 s3nsapis.fr
。
Go
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Resource Manager Go API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為 s3nsapis.fr
。
Java
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Resource Manager Java API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為 s3nsapis.fr
。
Python
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Resource Manager Python API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為 s3nsapis.fr
。
恭喜!您已使用 Resource Manager API 中的 IAM 方法,修改專案的存取權。
還順利嗎?
清除所用資源
後續步驟