使用用戶端程式庫授予角色
瞭解如何以您慣用的程式語言,開始使用 Resource Manager API 的 IAM 方法。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
建立 Google Cloud 專案
在這個快速入門導覽課程中,您需要使用新的 Google Cloud 專案。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google 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 example,myemail@example.com
.ROLE
: the IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google 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 example,myemail@example.com
.ROLE
: the IAM role that you grant to your user account.
- 初始化 Resource Manager 服務,該服務可管理專案。 Google Cloud
- 讀取專案的允許政策。
- 修改允許政策,將記錄檔寫入者角色 (
roles/logging.logWriter
) 授予您的 Google 帳戶。 - 寫入更新後的允許政策。
- 顯示具備專案層級記錄檔寫入者角色 (
roles/logging.logWriter
) 的所有主體。 - 撤銷記錄檔寫入者角色。
your-project
:您的專案 ID。your-member
:使用者帳戶的電子郵件地址。例如:user: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
- 請參閱身分與存取權管理的運作方式。
- 進一步瞭解如何授予、變更及撤銷存取權。
- 使用政策疑難排解工具排解存取權問題。
安裝用戶端程式庫
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 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Go
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Resource Manager Go API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Resource Manager Java API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Resource Manager Python API 參考說明文件。
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
恭喜!您已使用 Resource Manager API 中的 IAM 方法,修改專案的存取權。
還順利嗎?
清除所用資源
後續步驟