Telegram新设备登录:绕过PHONE_CODE_EXPIRED,用QR码代替验证码,含脚本和完整流程
用 Pyrogram/Telethon 通过手机号+验证码登录 Telegram 时:
PHONE_CODE_EXPIREDTelegram 的新设备登录安全机制:
注意: PHONE_CODE_EXPIRED 在这里是误导性错误,真实原因是"设备未批准",而非5分钟超时。
用 Telethon 的 qr_login() 方法,让用户直接扫QR,设备自动加入可信列表。
bash
# 创建独立 Python 环境(推荐3.12)
python3 -m venv /tmp/tg_env
source /tmp/tg_env/bin/activate
pip install telethon qrcode pillow
python
import asyncio, os, qrcode
from telethon import TelegramClient
API_ID = "你的api_id" # https://my.telegram.org
API_HASH = "你的api_hash" # 同上
SESSION_PATH = "/path/to/session" # session文件路径
OUT_PNG = "/tmp/tg_qr.png"
async def main():
client = TelegramClient(SESSION_PATH, API_ID, API_HASH)
print("Connecting...", flush=True)
await client.connect()
print("Connected. Generating QR...", flush=True)
# 获取QR码
qr_login = await client.qr_login()
url = qr_login.url
print(f"QR URL: {url}", flush=True)
# 生成PNG图片
img = qrcode.make(url)
img.save(OUT_PNG)
print(f"QR saved: {OUT_PNG}", flush=True)
# 等待扫码完成
print("Waiting for scan...", flush=True)
await qr_login.wait()
print("Scanned!", flush=True)
me = await client.get_me()
print(f"OK: {me.first_name} @{me.username} ID={me.id}", flush=True)
await client.disconnect()
asyncio.run(main())
/tmp/tg_qr.pngpython
# 发送图片给Telegram用户
with open("/tmp/tg_qr.png", "rb") as f:
await client.send_file("telegram_chat_id", f)
错误原因解决PHONE_CODE_EXPIRED设备未批准,被误判为超时用QR码登录PHONE_NUMBER_INVALID短时间内请求次数过多被封等2分钟再试PeerUser not found (删私聊时)对话已不存在跳过,无需处理QR扫描后立即过期网络问题或IP不稳定换个网络重试
清理可疑频道/陌生私聊:见 references/telegram-cleanup-workflow.md(含脚本 + 失败处理)
用户去 https://github.com/settings/tokens/new 创建 PAT(勾 gist scope),发给你后用 curl 直接调用 GitHub API:
bash
curl -s -X POST "https://api.github.com/gists" \
-H "Authorization: token <PAT>" \
-H "Content-Type: application/json" \
-d '{
"description": "<标题>",
"public": true,
"files": {
"telegram-qr-login.md": {"content": "..."}
}
}'
返回 html_url 即为发布链接。无需 gh CLI,无需登录。
ClawHub 需要独立的 GitHub OAuth 登录( Sign in with GitHub),不支持 GitHub PAT。
流程:
npm i -g clawhub(已装)clawhub login --token <token> 或浏览器交互方式认证clawhub skill publish <path> --slug <slug>注意:ClawHub skill 存储在 Convex 数据库,GitHub 仓库 openclaw/clawhub 的 skills/ 目录是模板,非实际发布路径。