Not able to access public API of twitter

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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)
</code>
<code>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) </code>
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.

New contributor

Tanya Sinha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật