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

使用instagrapi发送Instagram私信遇403错误求助

使用instagrapi发送私信时遇到403 Forbidden错误

我尝试使用instagrapi发送私信,参照文档编写了以下代码:

from instagrapi import Client

cl = Client()
cl.login(USERNAME, PASSWORD)

cl.direct_send("This is a message", ['53979279619', '54365742245', '51215837511', '53058609422', '54547827808'])

运行代码时出现如下错误:

requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: 

https://i.instagram.com/api/v1/direct_v2/threads/broadcast/text/

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bot.py", line 67, in <module>
    main(file_name)
  File "bot.py", line 42, in main
    cl.direct_send(f"{message}", followers_id)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instagrapi\mixins\direct.py", line 254, in direct_send
    result = self.private_request(
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instagrapi\mixins\private.py", line 447, in private_request
    raise e
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instagrapi\mixins\private.py", line 434, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instagrapi\mixins\private.py", line 326, in _send_private_request
    raise ClientForbiddenError(e, response=e.response, **last_json)
instagrapi.exceptions.ClientForbiddenError: {"action":"item_ack","status_code":"403","payload":{"client_context":"6800073986551502662","message":"This may be due to their app version or other settings."},"status":"fail"}

我确认账号未被封禁且处于活跃状态,但更换账号后仍出现相同错误,恳请协助解决该问题。


解决方法

  • 检查目标用户隐私设置:错误提示提到可能和对方设置有关,部分用户可能开启了「仅接受已关注用户的私信」,而你的账号未关注这些目标用户,导致被拒绝。先确认是否已关注所有收件人,或者对方是否允许陌生人发私信。
  • 模拟真实客户端环境:Instagram的反爬机制会识别非官方请求,初始化Client时添加设备模拟参数,同时处理可能的验证挑战:
    from instagrapi import Client
    from instagrapi.exceptions import ChallengeRequired
    from instagrapi.mixins.challenge import ChallengeChoice
    
    cl = Client()
    # 生成模拟设备信息,贴近真实客户端
    cl.set_device(cl.generate_device())
    try:
        cl.login(USERNAME, PASSWORD)
    except ChallengeRequired as e:
        # 处理验证挑战,选择短信或邮箱验证
        challenge = cl.challenge_resolve(e.challenge_url)
        if challenge["step_name"] == "select_verify_method":
            cl.challenge_select_verify_method(challenge, choice=ChallengeChoice.SMS)
            # 输入收到的验证码
            cl.challenge_send_security_code(input("请输入验证码: "))
            cl.login(USERNAME, PASSWORD)
    
  • 控制发送频率与批量大小:Instagram限制短时间内的批量操作,拆分发送任务并添加间隔:
    import time
    
    followers_id = ['53979279619', '54365742245', '51215837511', '53058609422', '54547827808']
    for user_id in followers_id:
        cl.direct_send("This is a message", [user_id])
        time.sleep(10)  # 每次发送间隔10秒,可根据情况调整
    
  • 更新instagrapi版本:旧版本库可能存在API适配问题,执行以下命令更新:
    pip install --upgrade instagrapi
    
  • 确认账号私信功能正常:先在官方APP或网页端手动发送一条私信,确保账号本身的私信功能未被隐形限制,再用代码尝试。

内容的提问来源于stack exchange,提问作者Gerald-x

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 16:27:25