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

使用Telethon API获取自有频道用户时遇权限错误求助

解决Telethon获取自有频道成员时的ChatAdminRequiredError问题

嘿,我完全懂你的困惑——明明是自己创建且身为管理员的频道,调用GetParticipantsRequest却触发权限错误,这确实有点反直觉。咱们一步步拆解问题,找到解决方案:

问题根源:频道与群组的权限机制差异

Telegram的**广播频道(Broadcast Channel)和超级群组(Supergroup)**在API权限逻辑上有细微区别:

  • 超级群组的成员列表权限相对宽松,只要你是管理员就能正常获取;
  • 但广播频道(那种单向发消息的频道)的成员访问逻辑更严格,即使你是创建者,手动构造InputChannel时如果参数传递不当,Telethon可能无法正确识别你的管理员权限,从而触发错误。

解决方案1:用更可靠的方式获取频道实体

你代码里手动通过ResolveUsernameRequest和InputChannel构造频道实体的方式容易出错,Telethon提供了更智能的client.get_entity()方法,能自动适配群组/频道类型,避免权限识别问题。修改后的代码如下:

from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch

def getUserNames(channel_identifier):
    offset = 0
    limit = 100
    all_participants = []
    selected_participants = []
    
    # 自动解析频道/群组实体,无需手动构造InputChannel
    channel = client.get_entity(channel_identifier)
    
    while True:
        participants = client(GetParticipantsRequest(
            channel, ChannelParticipantsSearch(''), offset, limit, 0
        ))
        if not participants.users:
            break
        all_participants.extend(participants.users)
        offset += len(participants.users)
    
    if all_participants:
        selected_participants = [x.username for x in all_participants if x.username]
        print(selected_participants)
        return selected_participants
    else:
        return -1

解决方案2:使用Telethon封装好的高级方法

Telethon提供了client.get_participants()方法,它会自动处理分页、权限校验等细节,比直接调用底层的GetParticipantsRequest更省心:

def getUserNames(channel_identifier):
    selected_participants = []
    # 自动处理所有分页逻辑,无需手动循环
    all_participants = client.get_participants(channel_identifier, search='', limit=None)
    if all_participants:
        selected_participants = [x.username for x in all_participants if x.username]
        print(selected_participants)
        return selected_participants
    else:
        return -1

额外排查点

  • 确认频道类型:如果是广播频道,检查频道设置里是否允许管理员查看成员(默认创建者拥有该权限,但偶尔会出现API状态同步延迟);
  • 验证登录会话:确保你的client实例是用拥有频道管理员权限的账号登录的,没有会话过期或切换账号的情况;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:01:13