使用Tweepy调用Twitter API v2遇403 Forbidden错误求助
解决Tweepy调用Twitter API v2返回403 Forbidden的问题
核心原因
根据错误提示When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project,你的开发者应用未关联到Twitter的Project,这是导致403的直接原因。
解决方案
1. 将应用关联到Project
- 登录Twitter开发者平台,进入「Projects & Apps」页面
- 若还没有Project,点击「Create Project」创建一个,按引导完成设置
- 将现有App迁移到该Project中,或直接在Project内创建新App
2. 重新生成并使用正确的令牌
- 应用关联Project后,进入App的「Keys and Tokens」页面,重新生成Bearer Token
search_recent_tweets采用App-only认证,仅需Bearer Token即可初始化Client,无需其他密钥:
bearer = config["BEARER"] # 简化Client初始化 client = tweepy.Client(bearer_token=bearer) tweets = client.search_recent_tweets(query, tweet_fields=["text"], max_results=100)
关于免费API的说明
Twitter目前仍保留免费API层,search_recent_tweets的免费额度为每月1500次请求,单次最多返回100条推文。当前403错误并非额度耗尽导致,而是应用未关联Project的认证问题。
内容的提问来源于stack exchange,提问作者MidKnight
相关产品推荐
相关产品推荐

