本页面介绍如何使用 Identity and Access Management (IAM) API、 Google Cloud 控制台和 gcloud 命令行工具列出和修改服务账号。
准备工作
- 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 服务账号 - 所需的角色- 如需获得管理服务账号所需的权限,请让管理员向您授予项目的以下 IAM 角色: - 
            查看服务账号:View Service Accounts (roles/iam.serviceAccountViewer)
- 
            修改服务账号:Service Account Admin (roles/iam.serviceAccountAdmin)
 - 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。 - 如需详细了解这些角色,请参阅服务账号角色。 - IAM 基本角色也具有管理服务账号的权限。 您不应在生产环境中授予基本角色,但可以在开发或测试环境中授予这些角色。 - 列出服务账号- 您可以列出项目中的用户管理的服务账号以帮助您审核服务账号和密钥,也可以列出用于管理服务账号的自定义工具。 - 您无法列出可能出现在项目的允许政策和审核日志中的服务代理。服务代理不在您的项目中,您无法直接访问它们。 - 控制台- 在 Google Cloud 控制台中,打开服务账号页面。 
- 选择一个项目。 - 服务账号页面列出了您所选项目中所有用户管理的服务账号。 
 - 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 list命令可列出项目中的所有服务账号。命令: gcloud iam service-accounts list 输出会列出项目中所有用户管理的服务账号: NAME EMAIL SA_DISPLAY_NAME_1 SA_NAME_1@PROJECT_ID.iam.gserviceaccount.com SA_DISPLAY_NAME_2 SA_NAME_2@PROJECT_ID.iam.gserviceaccount.com 
- PROJECT_ID:您的 Google Cloud 项目 ID。项目 ID 是字母数字字符串,例如- my-project。
 - 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.list方法可列出项目中的每个用户管理的服务账号。- 在使用任何请求数据之前,请先进行以下替换: - HTTP 方法和网址: - GET https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts - 如需发送您的请求,请展开以下选项之一: - 您应该收到类似以下内容的 JSON 响应: - { "accounts": [ { "name": "projects/my-project/serviceAccounts/sa-1@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "123456789012345678901", "email": "sa-1@my-project.iam.gserviceaccount.com", "description": "My first service account", "displayName": "Service account 1", "etag": "BwUpTsLVUkQ=", "oauth2ClientId": "987654321098765432109" }, { "name": "projects/my-project/serviceAccounts/sa-2@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "234567890123456789012", "email": "sa-2@my-project.iam.gserviceaccount.com", "description": "My second service account", "displayName": "Service account 2", "etag": "UkQpTwBVUsL=", "oauth2ClientId": "876543210987654321098" } ] }- 修改服务账号- 通常可通过服务账号的显示名(易记名称)和说明获知有关该服务账号的额外信息,例如服务账号的用途或该账号的联系人。 - 控制台- 在 Google Cloud 控制台中,打开服务账号页面。 
- 选择一个项目。 
- 点击要重命名的服务账号的电子邮件地址。 
- 在名称框中输入新名称,然后点击保存。 
 - 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 update命令可更新服务账号。命令: gcloud iam service-accounts update 
 SA_NAME@PROJECT_ID.iam.gserviceaccount.com
 --description="UPDATED_SA_DESCRIPTION"
 --display-name="UPDATED_DISPLAY_NAME"输出是重命名后的服务账号: description: UPDATED_SA_DESCRIPTION displayName: UPDATED_DISPLAY_NAME name: projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com 
- PROJECT_ID:您的 Google Cloud 项目 ID。项目 ID 是字母数字字符串,例如- my-project。
- SA_ID:您的服务账号的 ID。此 ID 可以是服务账号的电子邮件地址(格式为- SA_NAME@PROJECT_ID.iam.gserviceaccount.com)或服务账号的唯一数字 ID。
- SA_NAME:您的服务账号的字母数字 ID。此名称必须介于 6 到 30 个字符之间,且可以包含小写字母数字字符和短划线。
- 请至少替换以下其中一项:
- UPDATED_DISPLAY_NAME:您的服务账号的新显示名。
- UPDATED_DESCRIPTION:您的服务账号的新说明。
 - 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.patch方法可更新服务账号。- 在使用任何请求数据之前,请先进行以下替换: - HTTP 方法和网址: - PATCH https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_ID - 请求 JSON 正文: - { "serviceAccount": { "email": "SA_NAME@PROJECT_ID.iam.gserviceaccount.com", "displayName": "UPDATED_DISPLAY_NAME", "description": "UPDATED_DESCRIPTION" }, "updateMask": "displayName,description" }- 如需发送您的请求,请展开以下选项之一: - 您应该收到类似以下内容的 JSON 响应: - { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com", "displayName": "My updated service account", "description": "An updated description of my service account" }- 后续步骤- 了解如何停用和启用服务账号。
- 查看将 IAM 角色授予所有类型的主账号(包括服务账号)的流程。
- 了解如何使用角色建议来缩小所有主账号(包括服务账号)的权限范围。
- 了解如何将服务账号关联到资源。
 - 如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。 - 最后更新时间 (UTC):2025-10-21。