import tweepy
# API credentials
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_token_secret)
api = tweepy.API(auth)
# Search query
query = 'your_search_query'
# Initialize an empty list to store all tweets
all_tweets = []
# Pagination loop
max_tweets = 100 # Set a maximum number of tweets to retrieve
last_id = -1
while len(all_tweets) < max_tweets:
# Retrieve tweets
new_tweets = api.search_tweets(q=query, count=100, max_id=str(last_id - 1))
# If no more tweets are returned, break the loop
if not new_tweets:
break
# Add new tweets to the list
all_tweets.extend(new_tweets)
# Update the ID of the last tweet retrieved
last_id = new_tweets[-1].id
# Process and print all tweets
for tweet in all_tweets:
print(tweet.text)
I am getting this error
Traceback (most recent call last):
File “/Volumes/workplace/gen-ai/twitter.py”, line 24, in
new_tweets = api.search_tweets(q=query, count=100, max_id=str(last_id – 1))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/isunaina/.local/share/mise/installs/python/3.12/lib/python3.12/site-packages/tweepy/api.py”, line 33, in wrapper
return method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/isunaina/.local/share/mise/installs/python/3.12/lib/python3.12/site-packages/tweepy/api.py”, line 46, in wrapper
return method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/isunaina/.local/share/mise/installs/python/3.12/lib/python3.12/site-packages/tweepy/api.py”, line 1146, in search_tweets
return self.request(
^^^^^^^^^^^^^
File “/Users/isunaina/.local/share/mise/installs/python/3.12/lib/python3.12/site-packages/tweepy/api.py”, line 271, in request
raise Forbidden(resp)
tweepy.errors.Forbidden: 403 Forbidden
453 – You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product
Why is this error coming?
Is there any rule to access free API.What’s the problem?
I am expecting tweets from twitter , but getting error 403, forbidden.
Tanya Sinha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.