• 消息
  • 托管智能体
  • 管理
Search...
⌘K
CLI、SDK 和库
概览
ant CLI
快速入门身份验证选项使用 CLI脚本与自动化
客户端 SDK
中间件PythonTypeScriptC#GoJavaPHPRuby
库与集成
Apple Foundation ModelsOpenAI SDK 兼容性
Log in
身份验证选项
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
CLI、SDK 和库/ant CLI

CLI 身份验证选项

使用交互式登录、API 密钥、命名配置文件和 Workload Identity Federation 对 ant CLI 进行身份验证。

Was this page helpful?

  • 交互式登录
  • API 密钥
  • 检查身份验证状态
  • 在工作区之间切换
  • 管理配置文件

ant CLI 支持多种凭据来源。快速入门介绍了单命令的快捷路径(ant auth login)。本页将完整介绍所有选项。

交互式登录

ant auth login 让您无需创建或管理 API 密钥即可调用 API。它会针对 Claude Console 打开基于浏览器的 OAuth 流程,并将生成的凭据存储在 $ANTHROPIC_CONFIG_DIR 下(有关各操作系统的默认路径,请参阅配置目录)。在远程主机上或任何没有本地浏览器的环境中,传递 --no-browser 以打印授权 URL,然后将返回的代码粘贴回终端。

CLI
ant auth login

# 在没有浏览器的远程主机上:
ant auth login --no-browser

# 绑定到特定工作区并跳过浏览器选择器:
ant auth login --workspace-id wrkspc_01...

# 如果您通过 --profile 传递的命名配置文件不存在,
# 则会使用该名称创建一个新的命名配置文件。
ant auth login --profile <profile-name>

在浏览器流程中,您需要选择一个组织,然后选择一个 workspace(工作区)。颁发的令牌限定于该工作区,因此 CLI 只能看到属于该工作区的资源。传递 --workspace-id 可直接绑定并跳过选择器。如需在多个工作区中工作,请参阅在工作区之间切换。

交互式登录适用于本地开发以及在您自己的机器上编写脚本。对于 CI、服务器和容器等非交互式工作负载,请改用 Workload Identity Federation。

登录会将凭据写入 credentials/<profile>.json。配置文件的首次登录还会创建 configs/<profile>.json 并将其设置为活动配置文件。要删除已存储的凭据,请运行 ant auth logout,或运行 ant auth logout --all 以清除所有配置文件。

API 密钥

CLI 还会从 ANTHROPIC_API_KEY 环境变量读取您的 API 密钥。您可以从 Claude Console 获取密钥。

要为单次调用覆盖密钥,请传递 --api-key。要指向不同的 API 主机,请设置 ANTHROPIC_BASE_URL 或传递 --base-url。

检查身份验证状态

ant auth status 会打印 CLI 选择的凭据来源(API 密钥环境变量、OAuth 登录、联合身份验证或配置文件)、活动配置文件、活动令牌绑定的工作区以及配置目录路径。使用它来诊断工作负载为何选择了错误的凭据或工作区。

CLI
ant auth status
Active profile:  default
Config dir:      ~/.config/anthropic
Profile config:  ~/.config/anthropic/configs/default.json
Credentials:     ~/.config/anthropic/credentials/default.json

Credentials
  (active) * Profile (user_oauth) [via active_config]       sk-ant-oat01-EXA...
...

Workspace
  (active) * Workspace                                      wrkspc_01... (Engineering)

查看 (active) 行以了解哪个凭据来源和工作区生效。该命令报告的是状态而非执行健康检查,因此请勿基于退出状态编写脚本。有关凭据来源的完整排序,请参阅凭据优先级。

在工作区之间切换

交互式登录令牌绑定到单个工作区。要针对多个工作区使用 CLI,请在各自的命名配置文件下分别登录,然后在它们之间切换:

CLI
# 1. 创建配置文件(交互式;在浏览器中选择另一个工作区,
#    或传入 --workspace-id 以跳过选择器):
# ant auth login --profile other-ws

# 2. 将其设为后续命令的默认配置:
ant profile activate other-ws

# 3. 或仅为单个命令选择它,而不更改默认配置:
ant --profile other-ws models list
ANTHROPIC_PROFILE=other-ws ant models list

运行 ant auth status 以确认哪个配置文件和工作区处于活动状态。

只有在未设置 API 密钥时才会查询配置文件。如果您的环境中存在 ANTHROPIC_API_KEY,它会覆盖所有配置文件,这些命令都将使用该密钥所限定的工作区。在切换配置文件之前,请先取消设置该变量。

管理配置文件

ant profile 子命令可直接检查和编辑配置文件状态:

CLI
ant profile list
ant profile get --profile other-ws
ant profile set workspace_id wrkspc_01... --profile other-ws

ant profile set 的可写键包括 workspace_id、base_url、organization_id、scope、client_id 和 console_url。设置 workspace_id 会在配置文件配置中记录目标工作区,但不会重新绑定已颁发的凭据;请在该配置文件下再次运行 ant auth login,以为新工作区生成令牌。

有关配置文件的文件架构和联合身份验证块,请参阅配置文件配置文件。有关 Workload Identity Federation,请参阅身份验证概述和 WIF 参考。