本頁說明如何使用 Identity and Access Management (IAM) API、 Google Cloud 控制台和 gcloud CLI 停用及啟用服務帳戶。
事前準備
Enable the IAM API.
設定驗證方法。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
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.
-
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.
-
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」。
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 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.
-
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.
-
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.
-
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 的憑證。
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.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
瞭解 IAM 服務帳戶
必要的角色
如要取得管理服務帳戶所需的權限,請要求管理員授予您專案的「服務帳戶管理員」 (
roles/iam.serviceAccountAdmin
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。如要進一步瞭解這個角色,請參閱服務帳戶角色。
身分與存取權管理基本角色也包含管理服務帳戶的權限。您不應在正式版環境中授予基本角色,但可以在開發或測試環境中授予。
停用服務帳戶
就像刪除服務帳戶一樣,若您停用服務帳戶,應用程式就無法再透過該服務帳戶存取 Google Cloud 資源。如果您停用了預設的 App Engine 和 Compute Engine 服務帳戶,執行個體將無法再存取專案中的資源。若您嘗試停用已經停用的服務帳戶,將不會產生任何作用。
與刪除服務帳戶不同的地方,就是停用的服務帳戶可於必要時輕鬆重新啟用。我們建議您在刪除服務帳戶之前先將其停用,確認沒有重要應用程式使用該服務帳戶。
控制台
前往 Google Cloud 控制台的「Service accounts」(服務帳戶) 頁面。
選取專案。
按一下要停用的服務帳戶名稱。
在「服務帳戶狀態」下方,按一下「停用服務帳戶」,然後按一下「停用」確認變更。
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
執行
gcloud iam service-accounts disable
指令,停用服務帳戶。指令:
gcloud iam service-accounts disable SA_NAME@PROJECT_ID.iam.gserviceaccount.com
輸出:
Disabled service account SA_NAME@PROJECT_ID.iam.gserviceaccount.com
C++
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C++ API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
C#
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C# API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Go
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Go API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Java
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Java API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Python
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Python API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
REST
serviceAccounts.disable
方法會立即停用服務帳戶。使用任何要求資料之前,請先替換以下項目:
PROJECT_ID
:您的 Google Cloud 專案 ID。專案 ID 為英數字串,例如my-project
。SA_ID
:服務帳戶的 ID。 這可以是SA_NAME@PROJECT_ID.iam.gserviceaccount.com
形式的服務帳戶電子郵件地址,也可以是服務帳戶的不重複數字 ID。
HTTP 方法和網址:
POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_ID:disable
如要傳送要求,請展開以下其中一個選項:
如果成功,回應主體會留白。
啟用服務帳戶
啟用已停用的服務帳戶後,應用程式將能再次透過該服務帳戶存取Google Cloud 資源。
您可於需要時隨時啟用已停用的服務帳戶。若您嘗試啟用已經啟用的服務帳戶,將不會產生任何作用。
控制台
前往 Google Cloud 控制台的「Service accounts」(服務帳戶) 頁面。
選取專案。
按一下要啟用的服務帳戶名稱。
在「服務帳戶狀態」下方,按一下「啟用服務帳戶」,然後按一下「啟用」確認變更。
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
執行
gcloud iam service-accounts enable
指令以啟用服務帳戶。指令:
gcloud iam service-accounts enable SA_NAME@PROJECT_ID.iam.gserviceaccount.com
輸出:
Enabled service account SA_NAME@PROJECT_ID.iam.gserviceaccount.com
C++
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C++ API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
C#
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C# API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Go
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Go API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Java
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Java API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Python
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Python API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
REST
serviceAccounts.enable
方法可啟用先前停用的服務帳戶。使用任何要求資料之前,請先替換以下項目:
PROJECT_ID
:您的 Google Cloud 專案 ID。專案 ID 為英數字串,例如my-project
。SA_ID
:服務帳戶的 ID。 這可以是SA_NAME@PROJECT_ID.iam.gserviceaccount.com
形式的服務帳戶電子郵件地址,也可以是服務帳戶的不重複數字 ID。
HTTP 方法和網址:
POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_ID:enable
如要傳送要求,請展開以下其中一個選項:
如果成功,回應主體會留白。
後續步驟
- 瞭解如何刪除及取消刪除服務帳戶。
- 請參閱將 IAM 角色授予所有類型主體的程序,包括服務帳戶。
- 瞭解如何使用角色建議,縮減所有主體 (包括服務帳戶) 的權限。
- 瞭解如何將服務帳戶附加至資源。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-24 (世界標準時間)。
-