You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telethon输入正确验证码仍遭登录拦截问题求助

问题描述

我正在开发一个使用Telethon对接Telegram的Python项目,认证代码如下:

client = TelegramClient(phone, api_id, api_hash, flood_sleep_threshold=3)
client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input(f'Enter the code: {phone} : '))

输入正确验证码后,Telegram仍拦截登录,提示信息如下:

Telegram blocked an attempt to log in to your account from a new device on 23/10/2023 20:48:02 UTC.

Device: chatbot, 1.31.0, PC 64bit, Android, 4.18.0
Location: Germany (IP = .......)

No one could access your chats because the login was not completed. The code was entered correctly, but login was not allowed because this code was previously shared by your account.

请问有人遇到类似问题吗?能否告知原因及解决方法,以实现通过Telethon成功登录?


原因分析及解决方法

原因

  1. 验证码重复使用:Telegram登录验证码是一次性的,且与请求的设备、IP绑定。如果之前已用该验证码尝试过登录(哪怕失败),再次输入就会触发拦截。
  2. 设备/IP风险:你的客户端设备标识(代码里的chatbot设备名)或IP地址被Telegram标记为高风险,比如IP来自滥用高发地区,或者设备信息与官方客户端差异过大。

解决方法

  • 规范验证码使用流程:确保每次请求验证码后仅提交一次,避免重复输入。推荐使用Telethon内置的start()方法替代手动调用connect()和sign_in(),它会自动处理授权流程:
import asyncio
from telethon import TelegramClient

client = TelegramClient(phone, api_id, api_hash, flood_sleep_threshold=3)

async def main():
    await client.start(phone, code_callback=lambda: input(f'Enter the code: {phone} : '))
    # 后续业务逻辑

asyncio.run(main())
  • 调整设备标识参数:初始化TelegramClient时,设置更贴近官方客户端的设备信息,降低被拦截概率:
client = TelegramClient(
    phone,
    api_id,
    api_hash,
    flood_sleep_threshold=3,
    device_model="iPhone 14",
    system_version="iOS 17.0",
    app_version="10.1.0"
)
  • 更换低风险IP环境:如果当前IP属于Telegram风控名单,尝试切换到本地稳定网络或正规代理服务,避免使用被拉黑的IP段。

  • 手动授权设备:先在官方Telegram客户端登录账号,进入「设置→设备→最近的登录」,找到对应的未验证请求并手动确认权限,之后再用Telethon尝试登录。

内容的提问来源于stack exchange,提问作者Amin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:16:01