# 抖音开放 CLI 安装指南

支持 macOS、Linux 和 Windows 的 Intel/AMD 及 ARM64 平台。macOS/Linux 使用 `install.sh`，Windows 使用 `install.ps1`，均自动选择版本并安装配套 Skill。

## macOS / Linux 安装

```sh
tmp_dir="$(mktemp -d)"
curl --fail --location --proto '=https' --proto-redir '=https' \
  'https://lf-aihub.douyinpic.com/obj/developer/douyin-open-cli/install.sh' \
  --output "$tmp_dir/install.sh" &&
sh "$tmp_dir/install.sh"
```

默认安装位置：CLI 为 `~/.local/bin/douyin-open-cli`，Skill 为 `~/.agents/skills/douyin-open-cli/`。若命令无法找到，执行：

```sh
export PATH="$HOME/.local/bin:$PATH"
```

需要永久生效时，将此行加入 shell 的配置文件。自定义安装目录可向安装脚本传入 `--bin-dir` 和 `--skills-dir`。

投放渠道安装入口也使用同一套校验和事务安装流程：

```sh
# 将 aihsx 替换为 aihsx、kaka、zhendi、kpznx 或 syhcb
channel=aihsx
tmp_dir="$(mktemp -d)"
curl --fail --location --proto '=https' --proto-redir '=https' \
  "https://lf-aihub.douyinpic.com/obj/developer/douyin-open-cli/channels/$channel/install.sh" \
  --output "$tmp_dir/install.sh" &&
sh "$tmp_dir/install.sh"
```

安装器会把渠道写入用户配置目录的 `douyin-open-cli/channel`，后续普通升级保留该值。PC 设备 SDK 使用 `channel`；OpenAPI 和 Passport 账号请求每次读取一次安装渠道，同时填入 `app_channel` 和 `channel`，缺失或非法时两项均为 `cli`。MCS 埋点继续使用 `app_channel`。支持值为 `aihsx`（小红书创作者）、`kaka`（兴趣卡官号）、`zhendi`（兴趣卡小程序）、`kpznx`（开放平台站内信）、`syhcb`（商业化传播）。`DOUYIN_OPEN_CLI_CONFIG_DIR` 可指定绝对配置目录；不要在安装后随意改动，否则 CLI 无法读取之前的渠道标记。

## Windows 安装

在 Windows PowerShell 5.1 或 PowerShell 7 中下载安装脚本；查看并确认来源可信后执行。无需管理员权限或 Node.js：

```powershell
$installer = Join-Path $env:TEMP "douyin-open-cli-install.ps1"
Invoke-WebRequest -UseBasicParsing `
  "https://lf-aihub.douyinpic.com/obj/developer/douyin-open-cli/install.ps1" `
  -OutFile $installer
# 确认脚本内容和来源后执行；Bypass 只作用于这个进程，不修改系统执行策略。
powershell.exe -NoProfile -ExecutionPolicy Bypass -File $installer -AddToPath
```

遵守组织的脚本执行策略；被策略阻止时使用下方手动安装，不修改组织策略。CDN 上传新版脚本后，上述地址才可用。

默认安装位置：CLI 为 `%LOCALAPPDATA%\douyin-open-cli\bin\douyin-open-cli.exe`，Skill 为 `%USERPROFILE%\.agents\skills\douyin-open-cli/`。`-AddToPath` 仅追加用户级 PATH；重新打开终端及 Agent 后可使用 `douyin-open-cli`。不传该选项则 PATH 不变，可以直接用完整路径执行。

可指定目标目录，例如 Codex 的专用 Skill 目录（设置了 `CODEX_HOME` 时使用该目录下的 `skills`）：

```powershell
powershell.exe -NoProfile -ExecutionPolicy Bypass -File $installer `
  -SkillsDir "$HOME\.codex\skills" -AddToPath
```

`-BinDir` 自定义二进制目录，`-BaseUrl` 指定 HTTPS 发行目录，`-ArchiveDir` 从本地发行目录安装。`-Force` 更新已有的安装器管理文件，不覆盖未管理的 Skill 或手动安装的可执行文件。按 Agent 要求选择 Skill 目录，不另维护 Windows 版 Skill。

渠道安装可传 `-Channel`，例如 `-Channel kaka`；默认写入 `%APPDATA%\douyin-open-cli\channel`，也可用 `-ConfigDir` 或 `DOUYIN_OPEN_CLI_CONFIG_DIR` 指定绝对目录。

### Windows ZIP 手动安装

安装脚本不可用时，在 PowerShell 下载版本化 ZIP 并校验，然后解压到一个新目录：

```powershell
$base = "https://lf-aihub.douyinpic.com/obj/developer/douyin-open-cli"
$versionResponse = Invoke-WebRequest -UseBasicParsing `
  "https://open.douyin.com/api/douyin/v1/cli/version/"
$version = ($versionResponse.Content | ConvertFrom-Json).data.latest_version.Trim()
if ($version -notmatch '^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$') { throw "Invalid version" }
$arch = switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()) {
  "Arm64" { "arm64" }
  "X64" { "amd64" }
  default { throw "Unsupported CPU architecture" }
}
$name = "douyin-open-cli-$version-windows-$arch.zip"
Invoke-WebRequest -UseBasicParsing "$base/$name" -OutFile $name
$manifest = (Invoke-WebRequest -UseBasicParsing "$base/SHA256SUMS-$version").Content
$entry = $manifest -split "`n" | Where-Object { $_.Trim().EndsWith("  $name") }
if (@($entry).Count -ne 1) { throw "Missing or ambiguous checksum" }
$expected = ($entry.Trim() -split '\s+')[0]
if ((Get-FileHash -Algorithm SHA256 $name).Hash -ne $expected) { throw "Checksum mismatch" }
Expand-Archive -LiteralPath $name -DestinationPath ".\douyin-open-cli-$version"
& ".\douyin-open-cli-$version\douyin-open-cli.exe" --version
```

手动安装时，将解压出的 `skills/douyin-open-cli/` 放入所用 Agent 的 Skill 目录；不要覆盖自己维护的同名 Skill。下载包和校验文件来自同一来源，校验不能独立证明来源可信。

## 扫码登录

```sh
douyin-open-cli auth login
```

使用抖音扫描二维码并确认授权。已有有效且满足当前权限要求的登录状态时，直接复用。

Windows 加入 PATH 后也使用 `douyin-open-cli auth login`；当前目录运行时使用 `& .\douyin-open-cli.exe auth login`。根据返回的 `qr_image_path` 打开二维码；Agent 应直接展示该 PNG。

登录会申请本版本的全部默认权限。

Agent 可使用非阻塞流程：

```sh
douyin-open-cli auth login --begin
# 展示返回的 qr_image_path，用户确认后执行：
douyin-open-cli auth login --complete
```

若仍返回 `pending`，按 `retry_after_seconds` 等待后再次检查。

## 查看命令

```sh
douyin-open-cli -h
douyin-open-cli video -h
douyin-open-cli video list -h
douyin-open-cli video list --count 10
```

按需读取 Skill 中的子命令参考。写入操作需要用户确认并传入 `--yes`；可先用 `--dry-run` 预览。

## 更新

普通命令每 24 小时通过 `open.douyin.com` 的版本接口检测一次最新版本，发现新版后再从发行 CDN 下载和校验版本化压缩包。macOS/Linux 更新二进制和 Skill 后用新版执行原命令，无需另行确认。可修复的失败会刷新版本信息、重新下载并重试一次；仍失败则提示原因及手动升级方式，并由旧版继续执行原命令，不阻断使用。升级提示在 stderr，stdout 仍为业务结果。新版已开始执行后，不因业务报错回退重跑。Windows 暂不支持自升级，提示使用下面的 PowerShell 安装方式更新，同时继续执行旧版命令。

help、版本查询和 `self update --check` 不触发自动安装；开发构建不参与检测。离线不阻止业务使用，已经发现的新版本会保留供后续升级尝试。`DOUYIN_OPEN_CLI_NO_UPDATE_CHECK` 不再关闭检测。显式执行 `self update` 时，升级失败仍会返回错误。

```sh
douyin-open-cli --version
douyin-open-cli self update --check
douyin-open-cli self update
```

macOS/Linux 使用上述 `self update`；手动重新安装可传 `--force`。Windows 暂不支持 `self update`，重新下载最新 `install.ps1` 后执行下方命令，二进制和 Skill 一起更新；手动安装的用户重新下载 ZIP。

```powershell
powershell.exe -NoProfile -ExecutionPolicy Bypass -File $installer -Force -AddToPath
```

首次使用自定义目录时，更新也传入相同的 `-BinDir`、`-SkillsDir`。PowerShell 复杂 JSON 优先放在无 BOM 的 UTF-8 文件中，再用 `--data '@C:\path\request.json'` 传入；不要直接照搬 Bash 的 `\` 换行语法。
