I am very new to using the Twitter API and was testing some Python code (below)
from tweepy import OAuthHandler
from tweepy import Stream
import twitter_credentials
class StdOutListener(Stream):
def on_data(self, data):
print(data)
return True
def on_error(self, status):
print(status)
twitter_stream = StdOutListener(
twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_KEY_SECERET,
twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECERET
)
twitter_stream.filter(track=['#bitcoin'])
but whenever I try to run it, it would give this repeating error "Stream encountered HTTP error: 403". I checked the Twitter API response codes and error 403 was listed as the request is forbidden.
Here are some troubleshooting steps I already took:
- tried new access keys/consumer keys
- tried to create a new developer account
- uninstall and reinstall tweepy
None of these worked for me. So what is causing this error and how can I fix it? Thank you.
2
Answers
If you have Essential access, you won’t be able to access Twitter API v1.1.
See the FAQ section about this in Tweepy’s documentation for more information.
Use
StreamingClient
instead ofStreaming
from Tweepy if you only have Essential Access (Twitter API v2). The bearer token is enough for StreamingClient.For more information about its use:
https://docs.tweepy.org/en/stable/streamingclient.html#tweepy.StreamingClient