介绍
# x-api 🐦
使用官方 API (OAuth 1.0a) 发帖到 X。
## 何时使用
- 发布推文(基于 Cookie 的 `bird tweet` 会被机器人检测拦截) - 需要官方 API 访问以确保可靠性
对于**读取**(时间线、搜索、提及),请改用 `bird` CLI —— 它是免费的,且读取效果良好。
## 设置
### 1. 获取 API 凭据
1. 访问 https://developer.x.com/en/portal/dashboard 2. 创建一个 Project(项目)和 App(应用) 3. 将 App 权限设置为 **Read and Write**(读取和写入) 4. 从 "Keys and tokens"(密钥和令牌)选项卡获取您的密钥: - API Key (Consumer Key) - API Key Secret (Consumer Secret) - Access Token - Access Token Secret
### 2. 配置凭据
**选项 A:环境变量** ```bash export X_API_KEY="your-api-key" export X_API_SECRET="your-api-secret" export X_ACCESS_TOKEN="your-access-token" export X_ACCESS_SECRET="your-access-token-secret" ```
**选项 B:配置文件**,位于 `~/.clawdbot/secrets/x-api.json` ```json { "consumerKey": "your-api-key", "consumerSecret": "your-api-secret", "accessToken": "your-access-token", "accessTokenSecret": "your-access-token-secret" } ```
### 3. 安装依赖
```bash npm install -g twitter-api-v2 ```
## 发布推文
```bash x-post "Your tweet text here" ```
或使用完整路径: ```bash node /path/to/skills/x-api/scripts/x-post.mjs "Your tweet text here" ```
支持多行推文: ```bash x-post "Line one
Line two
Line three" ```
成功时返回推文 URL。
## 限制
- 免费版:1,500 条推文/月(需要在 X Developer Portal 中充值额度) - 基础版 ($100/月):更高的限制
## 读取(使用 bird)
用于读取、搜索和监控 —— 请使用 `bird` CLI:
```bash bird home # Timeline bird mentions # Mentions bird search "query" # Search bird user-tweets @handle # User's posts bird read <tweet-url> # Single tweet ```
## 故障排除
**402 Credits Depleted(额度已耗尽)**:在 X Developer Portal → Dashboard(仪表板)中添加额度。
**401 Unauthorized(未授权)**:重新生成 Access Token(确保首先设置了 Read+Write 权限)
**No credentials found(未找到凭据)**:设置环境变量或创建配置文件(参见上面的设置)