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

如何使用Tweepy API v2获取私人账号的提及消息?

解决Tweepy无法获取私人账号提及消息的问题

问题根源

Twitter API v2对私人账号(protected)的内容访问有严格限制:只有满足以下任一条件,第三方应用(你的机器人)才能获取到该私人账号的提及消息:

  • 机器人账号与该私人账号互相关注
  • 该私人账号通过OAuth 1.0a授权了你的机器人应用

具体解决步骤

  1. 互相关注验证
    让目标私人账号关注你的机器人账号,同时确保你的机器人账号也关注了该私人账号。完成后重新测试,大部分场景下这个方法就能解决问题。

  2. 使用用户上下文授权(OAuth 1.0a User Context)
    如果需要批量处理多个私人账号的提及,或者无法通过互关解决,可以引导私人账号授权你的应用:

    • 在Twitter开发者平台配置OAuth 1.0a的授权流程
    • 让私人账号通过授权链接登录,获取该账号的access_token和access_token_secret
    • 使用该账号的token调用API,即可获取其相关的提及内容(包括对机器人的提及)
  3. 代码优化与错误排查
    原代码缺少错误处理,建议添加异常捕获来明确权限问题:

    import tweepy
    import random
    import time
    
    def main():
        consumer_key = "你的consumer_key"
        consumer_secret = "你的consumer_secret"
        access_token = "你的access_token"
        access_token_secret = "你的access_token_secret"
        bearer_token = "你的bearer_token"
    
        client = tweepy.Client(
            consumer_key=consumer_key,
            consumer_secret=consumer_secret,
            access_token=access_token,
            access_token_secret=access_token_secret,
            bearer_token=bearer_token
        )
    
        bot_id = "你的机器人账号ID"  # 变量名修改为更清晰的bot_id
        last_mention = 0
        yesno = ['Y', 'N']
    
        while True:
            try:
                # 添加用户字段,可查看提及者是否为私人账号
                response = client.get_users_mentions(
                    id=bot_id,
                    since_id=last_mention,
                    user_fields=["protected"]  # 这里可以获取提及者的protected状态
                )
                mentions = response.data
                print(mentions)
    
                if mentions:
                    for mention in mentions:
                        tweet_id = mention.id
                        tweet_txt = mention.text
                        # 获取提及者的protected状态(可选,用于调试)
                        user_protected = next(user.protected for user in response.includes["users"] if user.id == mention.author_id)
    
                        if 'Y/N' in tweet_txt:
                            text = random.choice(yesno)  # 用random.choice更简洁
                            client.create_tweet(text=text, in_reply_to_tweet_id=tweet_id)
                            last_mention = tweet_id
                    time.sleep(5)
                else:
                    time.sleep(5)
            except tweepy.TweepyException as e:
                print(f"API调用错误: {e}")
                time.sleep(10)  # 出错后延长休眠时间
            except StopIteration:
                # 未找到对应用户时的处理
                pass
    
    if __name__ == "__main__":
        main()
    
    • 新增user_fields=["protected"]可以查看提及者是否为私人账号,方便调试
    • 添加异常捕获,能直接看到API返回的权限错误信息(比如403 Forbidden)
    • 用random.choice简化随机选择逻辑,优化代码可读性

额外说明

  • 不要尝试通过"扩展字段"绕过权限限制,Twitter API对私人内容的访问控制是强制的,无法通过字段参数突破
  • 如果仅使用Bearer Token调用get_users_mentions,只能获取公开账号的提及;必须使用OAuth 1.0a的用户token才能获取私人账号的相关内容(满足互关或授权条件时)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 11:45:29