User 模块(用户)¶
user 模块提供用户资料查询功能,支持通过用户名(screen_name)或数字 ID(rest_id)两种方式查询。
目录¶
user get — 通过用户名查询¶
通过 Twitter 用户名(@handle,不含 @)查询用户资料。
语法¶
参数¶
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
--screen-name <NAME> |
string | 是 | Twitter 用户名,不含 @(如 twitter,而非 @twitter) |
成功示例¶
输出:
{"success":true,"data":{"id":"783214","name":"Twitter","screen_name":"twitter","description":"What's happening?!","followers_count":65000000,"following_count":0,"tweet_count":15234,"listed_count":89123,"verified":true,"protected":false,"created_at":"2007-02-20T14:35:54Z","profile_image_url":"https://pbs.twimg.com/profile_images/880136122604507136/xHrnqf1T_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/783214/1594913664","location":"127.0.0.1","url":"https://about.twitter.com/"},"error":null,"meta":{"module":"user","action":"get","elapsed_ms":342,"timestamp":"2026-04-17T10:45:00Z","cli_version":"1.0.0","schema_version":"1.0"}}
失败示例¶
{"success":false,"data":null,"error":{"code":"NOT_FOUND","message":"用户 @nonexistent_xyz_abc 不存在或已停用","retryable":false,"retry_after_secs":null,"docs_url":null,"recovery_actions":["确认用户名拼写是否正确","检查用户是否已注销或被封禁"],"issue_url":null,"details":{"http_status":404}},"meta":{"module":"user","action":"get","elapsed_ms":189,"timestamp":"2026-04-17T10:46:00Z","cli_version":"1.0.0","schema_version":"1.0"}}
user get-by-id — 通过 ID 查询¶
通过 Twitter 数字 ID(rest_id)查询用户资料。当用户名可能变更时,使用 ID 更可靠。
语法¶
参数¶
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
--rest-id <ID> |
string | 是 | Twitter 用户数字 ID(snowflake 格式) |
成功示例¶
输出(结构与 user get 完全相同):
{"success":true,"data":{"id":"783214","name":"Twitter","screen_name":"twitter","description":"What's happening?!","followers_count":65000000,"following_count":0,"tweet_count":15234,"listed_count":89123,"verified":true,"protected":false,"created_at":"2007-02-20T14:35:54Z","profile_image_url":"https://pbs.twimg.com/profile_images/880136122604507136/xHrnqf1T_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/783214/1594913664","location":"127.0.0.1","url":"https://about.twitter.com/"},"error":null,"meta":{"module":"user","action":"get-by-id","elapsed_ms":287,"timestamp":"2026-04-17T10:45:00Z","cli_version":"1.0.0","schema_version":"1.0"}}
失败示例¶
{"success":false,"data":null,"error":{"code":"NOT_FOUND","message":"用户 ID 999999999999999 不存在","retryable":false,"retry_after_secs":null,"docs_url":null,"recovery_actions":["确认用户 ID 是否正确"],"issue_url":null,"details":{"http_status":404}},"meta":{"module":"user","action":"get-by-id","elapsed_ms":201,"timestamp":"2026-04-17T10:46:00Z","cli_version":"1.0.0","schema_version":"1.0"}}
screen_name 与 rest_id 选择指南¶
| 场景 | 推荐方式 | 原因 |
|---|---|---|
| 已知用户名,首次查询 | user get --screen-name |
直接、直观 |
| 存储/追踪特定用户 | user get-by-id --rest-id |
用户名可随时更改,ID 永不变 |
| 需要发私信 | 先用 user get 获取 ID |
DM 命令需要数字 ID |
| 批量处理用户列表 | 统一用 ID | 避免用户名变更导致失败 |
获取用户 ID 的流程:
# 通过用户名查询,提取 ID
user_id=$(twitter-cli --cookies-file cookies.txt user get \
--screen-name elonmusk | jq -r '.data.id')
echo "User ID: $user_id"
# 然后用 ID 发私信
twitter-cli --cookies-file cookies.txt dm send \
--user-id "$user_id" \
--text "Hello!"
返回结构说明¶
两个命令的 data 字段结构完全相同:
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 用户数字 ID(snowflake,永久唯一) |
name |
string | 显示名称(用户可随时修改) |
screen_name |
string | 用户名(@handle,可修改) |
description |
string | 个人简介 |
followers_count |
number | 粉丝数 |
following_count |
number | 关注数 |
tweet_count |
number | 发帖总数 |
listed_count |
number | 被列入 List 的次数 |
verified |
bool | 是否有蓝标认证(官方认证) |
protected |
bool | 是否受保护账号(私密推文) |
created_at |
string | 账号创建时间(ISO 8601) |
profile_image_url |
string | null | 头像 URL(_normal.jpg 为 48x48,去掉 _normal 为原图) |
profile_banner_url |
string | null | Banner 图 URL |
location |
string | null | 用户设置的地理位置(自填文字) |
url |
string | null | 个人主页链接(通过 Twitter 的 t.co 短链) |
注意事项¶
profile_image_url中的_normal后缀可替换为_400x400获取更大尺寸protected: true的账号:其他人无法看到其推文,私信权限也可能受限verified字段反映官方蓝标状态(Twitter Blue 订阅者也可能有蓝标,具体以 Twitter 规则为准)
jq 常用提取¶
# 只提取关键字段
twitter-cli --cookies-file cookies.txt user get --screen-name twitter | \
jq '{id: .data.id, name: .data.name, followers: .data.followers_count}'
# 检查账号是否受保护
twitter-cli --cookies-file cookies.txt user get --screen-name someone | \
jq '.data.protected'
# 批量查询多个用户(shell 循环)
for name in twitter elonmusk github; do
twitter-cli --cookies-file cookies.txt user get --screen-name "$name" | \
jq -r '[.data.screen_name, .data.id, (.data.followers_count | tostring)] | join("\t")'
done
常见错误¶
| 错误码 | 原因 | 解决方法 |
|---|---|---|
NOT_FOUND |
用户不存在、已注销或被封禁 | 确认用户名/ID 是否正确 |
AUTH_FAILED |
cookies 过期 | 重新导出 cookies |
RATE_LIMIT |
查询过于频繁 | 降低请求频率,等待 retry_after_secs |
INVALID_ARGS |
--screen-name 包含 @ 符号 |
去掉 @ 前缀 |