本页面介绍如何使用 Identity and Access Management (IAM) API、 Google Cloud 控制台和 gcloud 命令行工具来创建服务账号。
默认情况下,每个项目最多可以有 100 个控制对资源的访问权限的服务账号。如有必要,您可以申请增加配额。详细了解配额和限制。
准备工作
- Enable the IAM API. - Roles required to enable APIs - To enable APIs, you need the Service Usage Admin IAM role ( - roles/serviceusage.serviceUsageAdmin), which contains the- serviceusage.services.enablepermission. Learn how to grant roles.
- 设置身份验证。 - 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,然后使用您的用户凭证设置应用默认凭证。 - 安装 Google Cloud CLI。 - 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 - 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 - C++- 如需在本地开发环境中使用本页面上的 C++ 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。 - 安装 Google Cloud CLI。 - 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 - 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,然后使用您的用户凭证设置应用默认凭证。 - 安装 Google Cloud CLI。 - 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 - 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 - Java- 如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。 - 安装 Google Cloud CLI。 - 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 - 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 - Python- 如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。 - 安装 Google Cloud CLI。 - 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 - 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 - REST- 如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。 - 安装 Google Cloud CLI。 - 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 - 如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。 - 了解 IAM 服务账号 - 所需的角色- 如需获得创建服务账号所需的权限,请让您的管理员为您授予项目的 Create Service Accounts ( - roles/iam.serviceAccountCreator) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。- 如果您想要授予新创建的服务账号对项目的访问权限,您还需要 Project IAM Admin ( - roles/resourcemanager.projectIamAdmin) 角色。- 创建服务账号- 创建服务账号时,您必须提供字母数字 ID(在下面的示例中为 - SERVICE_ACCOUNT_NAME),例如- my-service-account。ID 必须介于 6 到 30 个字符之间,且可以包含小写字母数字字符和短划线。在服务账号创建完毕后,无法再更改其名称。- 服务账号的名称将显示在创建过程中预配的电子邮件地址中,格式为 - SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com。- 每个服务账号还有一个自动生成的永久性唯一数字 ID。 - 您还可以在创建服务账号时提供如下信息: - DESCRIPTION是可选的服务账号说明。
- DISPLAY_NAME是服务账号的易记名称。
- PROJECT_ID是您的 Google Cloud 项目的 ID。
 - 创建服务账号后,您可能需要等待 60 秒或更长时间才能使用该服务账号。出现这种行为的原因是读取操作是最终一致的;新服务账号可能需要一段时间才能显示。如果您在创建服务账号后立即尝试读取或使用服务账号,并且收到错误消息,则可以使用指数退避算法重试请求。 - 控制台- 在 Google Cloud 控制台中,前往创建服务账号页面。
  
  
  
  
  
 
  
其余步骤会显示在 Google Cloud 控制台中。 
- 选择一个 Google Cloud 项目。
- 输入要在 Google Cloud 控制台中显示的服务账号名称。Google Cloud 控制台会根据此名称生成服务账号 ID。如有必要,请修改 ID。此 ID 创建后便无法更改。 
- 可选:输入服务账号的说明。
- 如果您现在不想设置访问权限控制,请点击完成以完成服务账号的创建过程。如需立即设置访问权限控制,请点击创建并继续,然后继续执行下一步。
- 可选:选择要在项目上向服务账号授予的一个或多个 IAM 角色。
- 完成添加角色后,点击继续。
- 可选:在服务账号用户角色字段中,添加需要将服务账号关联到其他资源的成员。
- 可选:在服务账号管理员角色字段中,添加需要管理服务账号的成员。
- 点击完成以完成服务账号的创建过程。
 - 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 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命令向用户授予服务账号的 Service Account User 角色 (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:项目 ID
- SERVICE_ACCOUNT_NAME:服务账号的名称
- USER_EMAIL:用户的电子邮件地址
 
- PROJECT_ID:您的 Google Cloud 项目 ID。项目 ID 是字母数字字符串,例如- my-project。
- SA_NAME:您的服务账号的字母数字 ID。此名称必须介于 6 到 30 个字符之间,且可以包含小写字母数字字符和短划线。
- SA_DESCRIPTION:可选。服务账号说明。
- SA_DISPLAY_NAME:服务账号的人类可读名称。
 - 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。 - 后续步骤- 了解如何列出和修改服务账号。
- 查看将 IAM 角色授予所有类型的主账号(包括服务账号)的流程。
- 了解如何将服务账号关联到资源。
 - 如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。 - 最后更新时间 (UTC):2025-10-22。