Interacts with the X/Twitter API to post tweets and threads, read timelines, search content, and perform analysis. Supports OAuth 1.0a and 2.0 Bearer authentication.
How this skill is triggered — by the user, by Claude, or both
Slash command
/everything-claude-code:x-apiThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> **易变技能。** X API 端点、访问层级、配额和写入权限变更频繁。
易变技能。 X API 端点、访问层级、配额和写入权限变更频繁。 在引用速率限制或实现发布/搜索流程之前,验证当前的开发者文档和账户访问权限。
以编程方式与 X (Twitter) 交互,用于发布、阅读、搜索和分析。
最适合:读取密集型操作、搜索、公共数据。
# 环境设置
export X_BEARER_TOKEN="your-bearer-token"
import os
import requests
bearer = os.environ["X_BEARER_TOKEN"]
headers = {"Authorization": f"Bearer {bearer}"}
# 搜索最近推文
resp = requests.get(
"https://api.x.com/2/tweets/search/recent",
headers=headers,
params={"query": "claude code", "max_results": 10}
)
tweets = resp.json()
用于:发布推文、管理账户、私信和任何写入流程。
# 环境设置 — 使用前导出
export X_CONSUMER_KEY="your-consumer-key"
export X_CONSUMER_SECRET="your-consumer-secret"
export X_ACCESS_TOKEN="your-access-token"
export X_ACCESS_TOKEN_SECRET="your-access-token-secret"
旧版别名如 X_API_KEY、X_API_SECRET 和 X_ACCESS_SECRET 可能存在于旧设置中。在文档记录或连接新流程时,优先使用 X_CONSUMER_* 和 X_ACCESS_TOKEN_SECRET 名称。
import os
from requests_oauthlib import OAuth1Session
oauth = OAuth1Session(
os.environ["X_CONSUMER_KEY"],
client_secret=os.environ["X_CONSUMER_SECRET"],
resource_owner_key=os.environ["X_ACCESS_TOKEN"],
resource_owner_secret=os.environ["X_ACCESS_TOKEN_SECRET"],
)
resp = oauth.post(
"https://api.x.com/2/tweets",
json={"text": "来自 Claude Code 的问候"}
)
resp.raise_for_status()
tweet_id = resp.json()["data"]["id"]
def post_thread(oauth, tweets: list[str]) -> list[str]:
ids = []
reply_to = None
for text in tweets:
payload = {"text": text}
if reply_to:
payload["reply"] = {"in_reply_to_tweet_id": reply_to}
resp = oauth.post("https://api.x.com/2/tweets", json=payload)
tweet_id = resp.json()["data"]["id"]
ids.append(tweet_id)
reply_to = tweet_id
return ids
resp = requests.get(
f"https://api.x.com/2/users/{user_id}/tweets",
headers=headers,
params={
"max_results": 10,
"tweet.fields": "created_at,public_metrics",
}
)
resp = requests.get(
"https://api.x.com/2/tweets/search/recent",
headers=headers,
params={
"query": "from:affaanmustafa -is:retweet",
"max_results": 10,
"tweet.fields": "public_metrics,created_at",
}
)
resp = requests.get(
"https://api.x.com/2/tweets/search/recent",
headers=headers,
params={
"query": "from:affaanmustafa -is:retweet -is:reply",
"max_results": 25,
"tweet.fields": "created_at,public_metrics",
}
)
voice_samples = resp.json()
resp = requests.get(
"https://api.x.com/2/users/by/username/affaanmustafa",
headers=headers,
params={"user.fields": "public_metrics,description,created_at"}
)
# 媒体上传使用 v1.1 端点
# 步骤 1:上传媒体
media_resp = oauth.post(
"https://upload.twitter.com/1.1/media/upload.json",
files={"media": open("image.png", "rb")}
)
media_id = media_resp.json()["media_id_string"]
# 步骤 2:带媒体发布
resp = oauth.post(
"https://api.x.com/2/tweets",
json={"text": "看看这个", "media": {"media_ids": [media_id]}}
)
X API 速率限制因端点、认证方法和账户层级而异,且随时间变化。始终:
x-rate-limit-remaining 和 x-rate-limit-reset 头import time
remaining = int(resp.headers.get("x-rate-limit-remaining", 0))
if remaining < 5:
reset = int(resp.headers.get("x-rate-limit-reset", 0))
wait = max(0, reset - int(time.time()))
print(f"接近速率限制。{wait}秒后重置")
resp = oauth.post("https://api.x.com/2/tweets", json={"text": content})
if resp.status_code == 201:
return resp.json()["data"]["id"]
elif resp.status_code == 429:
reset = int(resp.headers["x-rate-limit-reset"])
raise Exception(f"速率限制。在 {reset} 时重置")
elif resp.status_code == 403:
raise Exception(f"禁止访问: {resp.json().get('detail', '检查权限')}")
else:
raise Exception(f"X API 错误 {resp.status_code}: {resp.text}")
.env 文件。.env 文件。 添加到 .gitignore。使用 brand-voice 加 content-engine 生成平台原生内容,然后通过 X API 发布:
语声档案content-engine 以 X 原生格式生成内容brand-voice — 从真实 X 和站点/源材料构建可复用的语声档案content-engine — 为 X 生成平台原生内容crosspost — 跨 X、LinkedIn 和其他平台分发内容connections-optimizer — 在起草网络驱动的外联之前重新组织 X 关系图npx claudepluginhub aaione/everything-claude-code-zhX/Twitter API integration for posting tweets and threads, reading timelines, searching, and analytics. Covers OAuth auth patterns and rate limits.
Integrates with X/Twitter API to post tweets and threads, read timelines and user data, search content, and retrieve analytics. Handles OAuth 1.0a/2.0 authentication and rate limits for programmatic use.
Enables Twitter/X interactions via 35 MCP Node.js tools: tweet/post threads, search/read tweets/timelines/mentions, manage likes/retweets/bookmarks/followers, update profile/bio/avatar/banner, upload media. Zero-config browser auth.