Spotify Controller Skill
Control Spotify playback from your AI agent using the official Spotify Web API.
This works across setups (local machine, Docker, VPS, and hybrid environments). It is especially useful for fixing Spotify control pain in headless VPS deployments. The server does not need a browser or a local Spotify client.
What this skill provides
- - A CLI workflow around INLINECODE0
- Playback control (
play, pause, next, prev) - Track lookup (
search) and quick play (playsearch) - Direct URI playback (
playtrack spotify:track:...) - Device management (
devices, setdevice) - Volume control (
volume 0-100, where supported)
Requirements
- - Python 3 available in runtime/container
- INLINECODE11 package installed
- Spotify Premium account
- Spotify Developer app credentials
- Environment variables:
-
SPOTIFY_CLIENT_ID
-
SPOTIFY_CLIENT_SECRET
- INLINECODE14
Install dependency:
CODEBLOCK0
(Alternative: pip install requests)
If you build OpenClaw in Docker, add this to your Dockerfile when requests is not already present:
CODEBLOCK1
Setup (Step-by-step)
1) Create a Spotify Developer App
- 1. Go to: https://developer.spotify.com/dashboard
- Click Create App
- Enter any app name/description
- Add Redirect URI:
-
http://127.0.0.1:8888/callback
- 5. Enable Web API access
- Save and copy:
-
Client ID
-
Client Secret
2) Get a refresh token (one-time, on local machine)
Open this URL in your browser (replace YOUR_CLIENT_ID):
CODEBLOCK2
Approve access, then copy the code value from the redirected URL.
Exchange code for tokens:
CODEBLOCK3
From response JSON, copy refresh_token.
INLINECODE22 is typically long-lived, but can be invalidated if app access is revoked, app settings change, or credentials rotate.
3) Add credentials to .env
CODEBLOCK4
4) Pass env vars to Docker compose service
In docker-compose.yml service environment: section:
CODEBLOCK5
5) Restart service/container
CODEBLOCK6
CODEBLOCK7
Usage
Run commands from workspace:
CODEBLOCK8
| Command | Description |
|---|
| INLINECODE26 | Show current playback state and track |
| INLINECODE27 |
Resume playback |
|
python3 spotify.py pause | Pause playback |
|
python3 spotify.py next | Skip to next track |
|
python3 spotify.py prev | Go to previous track |
|
python3 spotify.py volume 80 | Set volume (0–100) where supported |
|
python3 spotify.py search track | Search tracks (top results) |
|
python3 spotify.py playsearch "track" | Search and play first result |
|
python3 spotify.py playtrack spotify:track:URI | Play specific track URI |
|
python3 spotify.py devices | List available Spotify devices |
|
python3 spotify.py setdevice "BEDROOM-SPEAKER" | Set active device by name or id |
VPS / Headless behavior notes
- - Headless server control works because playback is executed on Spotify Connect devices (phone/desktop/web), not the server audio output.
- You still need at least one active Spotify device session.
If you see NO_ACTIVE_DEVICE:
- 1. Open Spotify on target device
- Start any track manually once
- Run INLINECODE38
- Retry command
Known Spotify API limitations (expected)
- - Some devices/content contexts may return
403 Restriction violated for play, prev, or other controls. - Some devices may reject remote volume changes (
VOLUME_CONTROL_DISALLOW). - Device handoff can lag; immediate
status after transfer may briefly show stale state.
These are Spotify-side constraints, not necessarily script bugs.
Operational guidance for automations
- - Treat non-zero exit codes as command failures.
- Validate environment vars at startup.
- Log command + status code for troubleshooting.
- Retry once for transient network errors.
- Don’t hardcode real credentials in files.
Security notes
- - Never commit
.env with live secrets. - Rotate app credentials if leaked.
- Use least-access scopes required for your workflow.
- The script only communicates with accounts.spotify.com and api.spotify.com
Quick troubleshooting checklist
- 1.
python3 spotify.py devices shows your target device? - Device is active in Spotify app?
- Env vars loaded inside container/runtime?
- Premium account confirmed?
- Refresh token still valid?
- Any Spotify 403 restriction reason in response?
Claim accuracy
- - Uses official Spotify Web API ✅
- Works on headless VPS ✅
- Practical for personal usage ✅
- Subject to normal Spotify API behavior/limits ✅
Spotify 控制器技能
通过官方 Spotify Web API 从您的 AI 代理控制 Spotify 播放。
此功能适用于各种环境(本地机器、Docker、VPS 和混合环境)。对于在无头 VPS 部署中解决 Spotify 控制痛点尤其有用。服务器无需浏览器或本地 Spotify 客户端。
本技能提供的功能
- - 围绕 spotify.py 的 CLI 工作流
- 播放控制(play、pause、next、prev)
- 曲目查找(search)和快速播放(playsearch)
- 直接 URI 播放(playtrack spotify:track:...)
- 设备管理(devices、setdevice)
- 音量控制(volume 0-100,在支持的情况下)
要求
- - 运行时/容器中可使用 Python 3
- 已安装 requests 包
- Spotify Premium 账户
- Spotify 开发者应用凭据
- 环境变量:
- SPOTIFY
CLIENTID
- SPOTIFY
CLIENTSECRET
- SPOTIFY
REFRESHTOKEN
安装依赖项:
bash
uv pip install requests --system
(替代方案:pip install requests)
如果您在 Docker 中构建 OpenClaw,且 requests 尚未存在,请将其添加到您的 Dockerfile 中:
dockerfile
RUN uv pip install requests --system
设置(分步指南)
1) 创建 Spotify 开发者应用
- 1. 访问:https://developer.spotify.com/dashboard
- 点击创建应用
- 输入任意应用名称/描述
- 添加重定向 URI:
- http://127.0.0.1:8888/callback
- 5. 启用 Web API 访问权限
- 保存并复制:
-
客户端 ID
-
客户端密钥
2) 获取刷新令牌(一次性操作,在本地机器上)
在浏览器中打开此 URL(将 YOURCLIENTID 替换为您的客户端 ID):
text
https://accounts.spotify.com/authorize?clientid=YOURCLIENTID&responsetype=code&redirect_uri=http://127.0.0.1:8888/callback&scope=user-modify-playback-state%20user-read-playback-state%20user-read-currently-playing
批准访问,然后从重定向的 URL 中复制 code 值。
用代码交换令牌:
bash
curl -s -X POST https://accounts.spotify.com/api/token \
-H Content-Type: application/x-www-form-urlencoded \
-d granttype=authorizationcode&code=YOURCODE&redirecturi=http://127.0.0.1:8888/callback&clientid=YOURCLIENTID&clientsecret=YOURCLIENTSECRET
从响应 JSON 中复制 refresh_token。
refresh_token 通常长期有效,但如果应用访问被撤销、应用设置更改或凭据轮换,可能会失效。
3) 将凭据添加到 .env
env
SPOTIFYCLIENTID=您的客户端ID
SPOTIFYCLIENTSECRET=您的客户端密钥
SPOTIFYREFRESHTOKEN=您的刷新令牌
4) 将环境变量传递给 Docker Compose 服务
在 docker-compose.yml 服务的 environment: 部分:
yaml
- - SPOTIFYCLIENTID=${SPOTIFYCLIENTID}
- SPOTIFYCLIENTSECRET=${SPOTIFYCLIENTSECRET}
- SPOTIFYREFRESHTOKEN=${SPOTIFYREFRESHTOKEN}
5) 重启服务/容器
bash
docker compose down
docker compose up -d openclaw-gateway
bash
chown <运行时用户>:<运行时组> /path/to/workspace/spotify.py
chmod 664 /path/to/workspace/spotify.py
使用方法
从工作区运行命令:
bash
python3 spotify.py <命令>
| 命令 | 描述 |
|---|
| python3 spotify.py status | 显示当前播放状态和曲目 |
| python3 spotify.py play |
恢复播放 |
| python3 spotify.py pause | 暂停播放 |
| python3 spotify.py next | 跳至下一曲目 |
| python3 spotify.py prev | 返回上一曲目 |
| python3 spotify.py volume 80 | 设置音量(0–100,在支持的情况下) |
| python3 spotify.py search track | 搜索曲目(显示最佳结果) |
| python3 spotify.py playsearch track | 搜索并播放第一个结果 |
| python3 spotify.py playtrack spotify:track:URI | 播放特定曲目 URI |
| python3 spotify.py devices | 列出可用的 Spotify 设备 |
| python3 spotify.py setdevice BEDROOM-SPEAKER | 按名称或 ID 设置活动设备 |
VPS / 无头环境行为说明
- - 无头服务器控制之所以可行,是因为播放是在 Spotify Connect 设备(手机/桌面/网页)上执行的,而不是服务器音频输出。
- 您仍然需要至少一个活动的 Spotify 设备会话。
如果看到 NOACTIVEDEVICE:
- 1. 在目标设备上打开 Spotify
- 手动开始播放任意曲目一次
- 运行 python3 spotify.py devices
- 重试命令
已知的 Spotify API 限制(预期内)
- - 某些设备/内容上下文可能对 play、prev 或其他控制返回 403 Restriction violated。
- 某些设备可能拒绝远程音量更改(VOLUMECONTROLDISALLOW)。
- 设备切换可能存在延迟;切换后立即执行 status 可能短暂显示过时状态。
这些是 Spotify 方面的限制,不一定是脚本错误。
自动化操作指南
- - 将非零退出代码视为命令失败。
- 在启动时验证环境变量。
- 记录命令和状态代码以便故障排除。
- 对于临时网络错误,重试一次。
- 不要在文件中硬编码真实凭据。
安全说明
- - 切勿提交包含真实密钥的 .env 文件。
- 如果凭据泄露,请轮换应用凭据。
- 使用工作流所需的最小权限范围。
- 该脚本仅与 accounts.spotify.com 和 api.spotify.com 通信。
快速故障排除清单
- 1. python3 spotify.py devices 是否显示您的目标设备?
- 设备在 Spotify 应用中是否处于活动状态?
- 环境变量是否已加载到容器/运行时中?
- 是否确认是 Premium 账户?
- 刷新令牌是否仍然有效?
- 响应中是否有任何 Spotify 403 限制原因?
声明准确性
- - 使用官方 Spotify Web API ✅
- 可在无头 VPS 上运行 ✅
- 适用于个人使用 ✅
- 受正常 Spotify API 行为/限制约束 ✅