本頁說明如何使用 Identity and Access Management (IAM) API、 Google Cloud 控制台和 gcloud
指令列工具建立服務帳戶。
根據預設,每個專案最多可有 100 個服務帳戶,用於控管資源存取權。如有需要,您可以申請提高配額。進一步瞭解配額與限制。
事前準備
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
-
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.
-
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.
-
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.
-
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.
-
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.
瞭解 IAM 服務帳戶
DESCRIPTION
是服務帳戶的選填說明。DISPLAY_NAME
是服務帳戶的好記名稱。PROJECT_ID
是 Google Cloud 專案 ID。- 前往 Google Cloud 控制台的「Create service account」(建立服務帳戶) 頁面。
其餘步驟會顯示在 Google Cloud 控制台。
- 選取 Google Cloud 專案。
- 輸入要顯示在 Google Cloud 控制台的服務帳戶名稱。
Google Cloud 控制台會根據這個名稱產生服務帳戶 ID。請視需要編輯 ID,ID 設定後即無法變更。
- 選用:輸入服務帳戶的說明。
- 如果您現在不想設定存取權控管機制,請按一下「Done」(完成) 以完成服務帳戶建立程序。如要立即設定存取權控管,請按一下「Create and continue」(建立並繼續),然後繼續進行下一個步驟。
- 選用:選擇一或多個 IAM 角色以授予專案的服務帳戶。
- 新增角色之後,請按一下「Continue」(繼續)。
- 選用:在「Service account users role」(服務帳戶使用者角色) 欄位中,新增需要將服務帳戶附加至其他資源的成員。
- 選用:在「Service account admins role」(服務帳戶管理員角色) 欄位中,新增需要管理服務帳戶的成員。
- 按一下「Done」(完成),即完成建立服務帳戶。
-
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 create
指令:gcloud iam service-accounts create SERVICE_ACCOUNT_NAME \ --description="DESCRIPTION" \ --display-name="DISPLAY_NAME"
替換下列值:
-
SERVICE_ACCOUNT_NAME
:服務帳戶名稱 -
DESCRIPTION
:服務帳戶的選填說明 -
DISPLAY_NAME
:要在 Google Cloud 控制台中顯示的服務帳戶名稱
-
-
選用:如要授予服務帳戶專案的 IAM 角色,請執行
gcloud projects add-iam-policy-binding
指令:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role="ROLE_NAME"
替換下列值:
-
PROJECT_ID
:專案 ID -
SERVICE_ACCOUNT_NAME
:服務帳戶名稱 -
ROLE_NAME
:角色名稱,例如roles/compute.osLogin
-
-
選用:如要允許使用者將服務帳戶附加至其他資源,請執行
gcloud iam service-accounts add-iam-policy-binding
指令,在服務帳戶上授予使用者服務帳戶使用者角色 (roles/iam.serviceAccountUser
):gcloud iam service-accounts add-iam-policy-binding \ SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --member="user:USER_EMAIL" \ --role="roles/iam.serviceAccountUser"
替換下列值:
PROJECT_ID
:專案 IDSERVICE_ACCOUNT_NAME
:服務帳戶名稱USER_EMAIL
:使用者的電子郵件地址
PROJECT_ID
:您的 Google Cloud 專案 ID。專案 ID 為英數字串,例如my-project
。SA_NAME
:服務帳戶的英數字元 ID。名稱長度必須介於 6 至 30 個字元之間,可以使用小寫英數字元和破折號。SA_DESCRIPTION
:選用。服務帳戶的說明。SA_DISPLAY_NAME
:服務帳戶的名稱,方便使用者辨識。- 瞭解如何列出及編輯服務帳戶。
- 請參閱將 IAM 角色授予所有類型主體的程序,包括服務帳戶。
- 瞭解如何將服務帳戶附加至資源。
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,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
C++
如要在本機開發環境中使用本頁的 C++ 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 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」。
必要的角色
如要取得建立服務帳戶所需的權限,請要求管理員授予您專案的「建立服務帳戶」 (
roles/iam.serviceAccountCreator
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。如要授予新建立的服務帳戶專案存取權,您也需要專案 IAM 管理員 (
roles/resourcemanager.projectIamAdmin
) 角色。建立服務帳戶
建立服務帳戶時,您必須提供英數字元 ID (以下範例中的
SERVICE_ACCOUNT_NAME
),例如my-service-account
。ID 長度必須介於 6 至 30 個字元之間,可以使用小寫英數字元和破折號。服務帳戶建立完成後,您就無法變更名稱。服務帳戶名稱會出現在建立期間佈建的電子郵件地址中,格式為
SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
。每個服務帳戶也都有永久且不重複的數字 ID,系統會自動產生這組 ID。
建立服務帳戶時,您也需要提供下列資訊:
建立服務帳戶後,可能需要等待 60 秒以上才能使用該帳戶。發生這種情況的原因是讀取作業具有最終一致性,因此需要一段時間才能顯示新的服務帳戶。如果您在建立服務帳戶後立即嘗試讀取或使用該帳戶,但收到錯誤訊息,可以使用指數輪詢重試要求。
控制台
gcloud
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.create
方法會建立服務帳戶。使用任何要求資料之前,請先替換以下項目:
HTTP 方法和網址:
POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts
JSON 要求主體:
{ "accountId": "SA_NAME", "serviceAccount": { "description": "SA_DESCRIPTION", "displayName": "SA_DISPLAY_NAME" } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "123456789012345678901", "email": "my-service-account@my-project.iam.gserviceaccount.com", "displayName": "My service account", "etag": "BwUp3rVlzes=", "description": "A service account for running jobs in my project", "oauth2ClientId": "987654321098765432109" }
建立好服務帳戶之後,為該服務帳戶授予一或多個角色,使其能夠代表您執行作業。
此外,如果服務帳戶需要存取其他專案中的資源,您通常必須在建立服務帳戶的專案中啟用這些資源的 API。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-09 (世界標準時間)。
-