“ 该技能详细介绍了如何使用 X(原 Twitter)API 进行编程交互。内容涵盖 OAuth 1.0a 和 OAuth 2.0 两种认证方式,并提供 Python 代码示例,实现发布推文/推文串、读取用户时间线、搜索内容、获取用户信息以及上传媒体等核心操作。无论是构建自动化机器人、进行社交媒体数据分析,还是将 X 功能集成到您的应用中,本指南都提供了完整的技术实现路径和最佳实践参考。”

通过编程方式与 X(原 Twitter)进行交互,实现发帖、读取、搜索和分析功能。
最适合:读取密集型操作、搜索、公开数据。
# 环境设置export X_BEARER_TOKEN="your-bearer-token"import osimport requestsbearer = 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_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-secret"import osfrom requests_oauthlib import OAuth1Sessionoauth = OAuth1Session(os.environ["X_API_KEY"],client_secret=os.environ["X_API_SECRET"],resource_owner_key=os.environ["X_ACCESS_TOKEN"],resource_owner_secret=os.environ["X_ACCESS_SECRET"],)
resp = oauth.post("https://api.x.com/2/tweets",json={"text": "Hello from Claude Code"})resp.raise_for_status()tweet_id = resp.json()["data"]["id"]
def post_thread(oauth, tweets: list[str]) -> list[str]:ids = []reply_to = Nonefor 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)resp.raise_for_status()tweet_id = resp.json()["data"]["id"]ids.append(tweet_id)reply_to = tweet_idreturn 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/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": "Check this out", "media": {"media_ids": [media_id]}})
X API 的速率限制因端点、认证方法和账户等级而异,并且会随时间变化。请务必:
x-rate-limit-remaining 和 x-rate-limit-reset 响应头import timeremaining = 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"Rate limit approaching. Resets in {wait}s")
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"Rate limited. Resets at {reset}")elif resp.status_code == 403:raise Exception(f"Forbidden: {resp.json().get('detail', 'check permissions')}")else:raise Exception(f"X API error {resp.status_code}: {resp.text}")
.env 文件。.env 文件。.gitignore。使用 content-engine 技能生成平台原生内容,然后通过 X API 发布:
content-enginecrosspost请在微信客户端打开
安装命令
npx skills add https://github.com/affaan-m/everything-claude-code --skill x-api每周安装数:6
代码仓库:https://github.com/affaan-m/everything-claude-code
GitHub 星标数:73.5K
首次出现:今天
安全审计:Gen Agent Trust HubPass SocketPass SnykWarn
安装于:codex5,opencode3,gemini-cli3,claude-code3,github-copilot3,kimi-cli3
更多技能>>>
find-skills 技能搜索工具 - 让AI更智能的skill
Skills之Python设计模式与最佳实践指南 - 构建健壮高效代码 GitHub Stars 17.4万+
Skills之创业公司团队构成分析:种子轮到A轮招聘、薪酬与股权分配指南 GitHub Stars 35K+
Skills之OpenAI图像生成技能:AI绘图、图片编辑与批量生成,支持网站素材、UI设计、产品模型 GitHub Stars 1.7万+
Skills之Google Stitch UI设计提示指南:AI驱动UI生成工具使用技巧与最佳实践 GitHub Stars 3.5万+
Skills之Excel财务建模规范与xlsx文件处理指南:专业格式、零错误公式与数据分析 GitHub Stars 12万+
Skills之办公自动化工作流套件:LibreOffice与Microsoft Office文档创建、电子表格自动化、演示文稿生成与格式转换 GitHub Stars 3.5+
