---
title: CLI 驗證選項
url: https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/authentication
description: 使用互動式登入、API 金鑰、具名設定檔和 Workload Identity Federation 來驗證 ant CLI。
---

`ant` CLI 支援多種憑證來源。[快速入門](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/quickstart#authentication)涵蓋了單一指令的順暢路徑（`ant auth login`）。本頁面完整涵蓋每個選項。

## 互動式登入

`ant auth login` 讓您無需建立或管理 API 金鑰即可呼叫 API。它會針對 Claude Console 開啟一個基於瀏覽器的 OAuth 流程，並將產生的憑證儲存在 `$ANTHROPIC_CONFIG_DIR` 下（請參閱[設定目錄](https://platform.claude.com/docs/zh-TW/manage-claude/wif-reference#configuration-directory)以了解作業系統特定的預設值）。在遠端主機上或任何沒有本機瀏覽器的環境中，請傳遞 `--no-browser` 以列印授權 URL，並將返回的代碼貼回終端機。

```bash CLI
ant auth login

# 在沒有瀏覽器的遠端主機上：
ant auth login --no-browser

# 綁定至特定工作區並略過瀏覽器選擇器：
ant auth login --workspace-id wrkspc_01...

# 如果您透過 --profile 傳入的具名設定檔不存在，
# 將會以該名稱建立一個新的具名設定檔。
ant auth login --profile work
```

在瀏覽器流程中，您會選擇一個組織，然後選擇一個[工作區](https://platform.claude.com/docs/zh-TW/manage-claude/workspaces)。發行的權杖[限定於該工作區](https://platform.claude.com/docs/zh-TW/manage-claude/workspaces#api-keys-and-resource-scoping)，因此 CLI 只能看到屬於該工作區的資源。傳遞 `--workspace-id` 以直接綁定並跳過選擇器。若要在多個工作區中工作，請參閱[在工作區之間切換](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/authentication#switch-between-workspaces)。

互動式登入適用於您自己機器上的本機開發和腳本編寫。對於非互動式工作負載，例如 CI、伺服器和容器，請改用 [Workload Identity Federation](https://platform.claude.com/docs/zh-TW/manage-claude/workload-identity-federation)。

登入會將憑證寫入 `credentials/<profile>.json`。設定檔的首次登入也會建立 `configs/<profile>.json` 並將其設為作用中的設定檔。若要移除已儲存的憑證，請執行 `ant auth logout`，或執行 `ant auth logout --all` 以清除每個設定檔。

## 管理員存取

預設情況下，`ant auth login` 會請求一個限定於工作區的權杖。若要管理 [Admin API](https://platform.claude.com/docs/zh-TW/manage-claude/admin-api) 頁面上記載的資源，請在專用設定檔下請求 `org:admin` 範圍：

```bash CLI
ant auth login --profile admin --scope "org:admin"

# 印出用於 Authorization 標頭的 bearer token：
ant auth print-credentials --profile admin --access-token
```

`org:admin` 範圍僅授予具有管理員、擁有者或主要擁有者角色的組織成員。發行的權杖具有組織範圍的存取權，且設定檔上的任何工作區綁定都不會限制它。請將管理員設定檔與您的日常設定檔分開，這樣例行指令就永遠不會以提升的存取權執行。

## API 金鑰

CLI 也會從 `ANTHROPIC_API_KEY` 環境變數讀取您的 API 金鑰。請從 [Claude Console](https://platform.claude.com/settings/keys) 取得金鑰。

<Tabs>
  <Tab title="zsh">
    ```bash
    echo 'export ANTHROPIC_API_KEY=sk-ant-api03-...' >> ~/.zshrc
    source ~/.zshrc
    ```
  </Tab>

  <Tab title="bash">
    ```bash
    echo 'export ANTHROPIC_API_KEY=sk-ant-api03-...' >> ~/.bashrc
    source ~/.bashrc
    ```
  </Tab>

  <Tab title="Windows">
    ```powershell
    setx ANTHROPIC_API_KEY "sk-ant-api03-..."
    ```

    開啟一個新的終端機以使變更生效。
  </Tab>
</Tabs>

若要為單次呼叫覆寫金鑰，請傳遞 [`--api-key-stdin`](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/using#global-flags) 並將金鑰經由管線傳給指令。若要指向不同的 API 主機，請設定 `ANTHROPIC_BASE_URL` 或傳遞 `--base-url`。

如果您使用的是限定於多個工作區的 API 金鑰，例如[個人或服務帳戶金鑰](https://platform.claude.com/docs/zh-TW/manage-claude/authentication#key-types)，您必須[指定工作區](https://platform.claude.com/docs/zh-TW/manage-claude/authentication#select-a-workspace)來執行您的指令。透過設定 `ANTHROPIC_WORKSPACE_ID` 環境變數（CLI 會自動讀取）來執行此操作，或使用 [`--workspace-id` 旗標](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/using#global-flags)。該值必須是 `wrkspc_...` ID。

```bash CLI
ant messages create \
  --workspace-id wrkspc_01... \
  --model claude-opus-5-5 \
  --max-tokens 1024 \
  --message '{role: user, content: "Hello, Claude"}'
```

## 檢查驗證狀態

`ant auth status` 會列印 CLI 選擇的憑證來源（API 金鑰環境變數、OAuth 登入、聯合或設定檔）、作用中的設定檔、作用中權杖綁定的工作區，以及設定目錄路徑。使用它來診斷為什麼工作負載選擇了錯誤的憑證或工作區。

```bash CLI
ant auth status
```

```text
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)` 列以查看哪個憑證來源和工作區勝出。該指令報告狀態而非執行健康檢查，因此請勿針對結束狀態編寫腳本。有關憑證來源的完整排序，請參閱[憑證優先順序](https://platform.claude.com/docs/zh-TW/manage-claude/wif-reference#credential-precedence)。

## 在工作區之間切換

互動式登入權杖綁定到單一工作區。若要針對多個工作區使用 CLI，請在各自的具名設定檔下登入每個工作區，然後在它們之間切換：

```bash CLI
# 1. 建立設定檔（互動式；在瀏覽器中選擇另一個 workspace，
#    或傳入 --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`](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/authentication#check-authentication-status) 以確認哪個設定檔和工作區處於作用中狀態。

<Note>
  環境中的 `ANTHROPIC_API_KEY` 或 `ANTHROPIC_AUTH_TOKEN` 優先於所有設定檔（請參閱[憑證優先順序](https://platform.claude.com/docs/zh-TW/manage-claude/wif-reference#credential-precedence)）。在您取消設定之前，無論您選擇哪個設定檔，API 請求都會使用該憑證，且 `ant` 會在 stderr 印出說明此情況的訊息。
</Note>

## 管理設定檔

`ant profile` 子指令可直接檢查和編輯設定檔狀態：

```bash 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` 以為新工作區鑄造權杖。

有關設定檔檔案結構描述和聯合區塊，請參閱[設定檔配置檔案](https://platform.claude.com/docs/zh-TW/manage-claude/wif-reference#profile-configuration-file)。有關 Workload Identity Federation，請參閱[驗證概觀](https://platform.claude.com/docs/zh-TW/manage-claude/authentication)和 [WIF 參考](https://platform.claude.com/docs/zh-TW/manage-claude/wif-reference)。

## 後續步驟

<CardGroup cols={3}>
  <Card title="使用 CLI" icon="terminal" href="https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/using">
    指令結構、輸出格式、GJSON 轉換和請求主體
  </Card>

  <Card title="CLI 腳本編寫與自動化" icon="code" href="https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/cli/scripting">
    版本控制 API 資源、腳本編寫模式，以及從 Claude Code 使用
  </Card>

  <Card title="Workload Identity Federation" icon="cloud" href="https://platform.claude.com/docs/zh-TW/manage-claude/workload-identity-federation">
    用於 CI、伺服器和容器的非互動式驗證
  </Card>
</CardGroup>
