大多數 Google Cloud 資源都會公開 testIamPermissions()
方法,可讓您以程式輔助方式檢查目前已驗證的呼叫者,是否已獲得資源的一或多個特定 IAM 權限。testIamPermissions()
方法會將資源 ID 和一組權限視為輸入參數,並傳回已授予呼叫者的權限組合。
您可以使用 testIamPermissions()
方法,判斷使用者是否應有權存取網頁應用程式中的管理工具。舉例來說,您可以根據使用者的權限,使用這個方法決定是否要顯示資源的詳細資訊。 Google Cloud
舉例來說,如要判斷目前已驗證的使用者是否擁有刪除專案的權限,請提供專案 ID (例如 foo-project
) 和 resourcemanager.projects.delete
權限做為輸入參數,呼叫 projects.testIamPermissions()
方法。如果呼叫者已獲得 resourcemanager.projects.delete
權限,該權限會列在回應主體中。如果呼叫者沒有這個權限,回應主體中不會列出任何權限。
testIamPermissions()
方法適用於第三方圖形使用者介面 (GUI),這些介面必須根據已驗證使用者擁有查看權限的項目來顯示 Google Cloud 資源。舉例來說,Google Cloud 主控台會在內部使用 testIamPermissions()
方法,決定驗證後您可以看到的資源和功能。通常會為不同的使用者授予不同的權限,且 Google Cloud 主控台會相應地隱藏或公開項目。
事前準備
Enable the Resource Manager API.
設定驗證方法。
Select the tab for how you plan to use the samples on this page:
C#
如要在本機開發環境中使用本頁的 .NET 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
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.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
C++
如要在本機開發環境中使用本頁的 C++ 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
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.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
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.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
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.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
必要的角色
測試權限時不需要 IAM 角色。
如何測試權限
這個範例說明如何測試 Google Cloud 專案的
resourcemanager.projects.get
和resourcemanager.projects.delete
權限。如要測試其他 Google Cloud 資源的權限,請使用每個資源公開的testIamPermissions()
方法。舉例來說,您可以測試 Cloud Storage 值區的 IAM 權限。C++
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C++ API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
C#
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。
IAM 會測試您用來產生憑證的服務帳戶權限。
Java
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。
IAM 會測試您用來產生憑證的服務帳戶權限。
Python
如要向 Resource Manager 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
如要瞭解如何安裝及使用 Resource Manager 的用戶端程式庫,請參閱這篇文章。
IAM 會測試您用來產生憑證的服務帳戶權限。
REST
在這個範例中,使用者擁有 IAM 角色,可取得專案相關資訊,但無法刪除專案。
Resource Manager API 的
projects.testIamPermissions
方法會接受權限清單,並測試主體擁有哪些權限。使用任何要求資料之前,請先替換以下項目:
PROJECT_ID
:您的 Google Cloud 專案 ID。專案 ID 為英數字串,例如my-project
。
HTTP 方法和網址:
POST https://cloudresourcemanager.googleapis.com/v1/projects/PROJECT_ID:testIamPermissions
JSON 要求主體:
{ "permissions": [ "resourcemanager.projects.get", "resourcemanager.projects.delete" ] }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "permissions": [ "resourcemanager.projects.get" ] }
後續步驟
瞭解如何授予、變更及撤銷主體的存取權。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-09 (世界標準時間)。
-