Stream encountered HTTP error: 403 Twitter API, but I have elevated access

Stream encountered HTTP error: 403 Twitter API, but I have elevated access

我最近获得了对 Twitter 开发人员的提升权限。我创建了一个新项目,并且打开了 OAuth 1.0a 并具有读写权限,但是当我 运行 代码时,我收到:Stream encountered HTTP error: 403

import tweepy

from config import ACCESS_TOKEN as access_token
from config import ACCESS_TOKEN_SECRET as access_token_secret
from config import API_KEY as api_key
from config import API_KEY_SECRET as api_key_secret

auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

class Linstener(tweepy.Stream):
    tweets = []
    limit = 1

    def on_status(self, status):
        self.tweets.append(status)
        # print(status.user.screen_name + ": " + status.text)

        if len(self.tweets) == self.limit:
            self.disconnect()


stream_tweet = Linstener(api_key, api_key_secret, access_token, access_token_secret)

users = ['pawka322']
user_ids = []

for user in users:
    user_ids.append(api.get_user(screen_name=user).id)

stream_tweet.filter(follow=user_ids)

我做了什么:

  1. 创建了一个新应用并保存了消费者密钥和消费者秘密
  2. 创建了一个新的开发项目
  3. 已启用 OAuth 1.0a:

如果我从用户时间线获取推文,我的凭据可以正常工作

如果您的应用是在 2022 年 4 月 29 日或之后创建的,您将无法通过 Twitter 访问流式传输 API v1.1:

Additionally, beginning today, new client applications will not be able to gain access to v1.1 statuses/sample and v1.1 statuses/filter.

https://twittercommunity.com/t/deprecation-announcement-removing-compliance-messages-from-statuses-filter-and-retiring-statuses-sample-from-the-twitter-api-v1-1/170500

您必须改用 Twitter API v2。
Tweepy 使用 Twitter API v2 进行流式传输的界面是 StreamingClient.