I want to retrieve tweets using Twitter API of basic developer account. I do not have too much knowledge of using API for scraping. I already set my Authentication Token's permission to Read and Write as well as write a possible correct code for retrieving tweets. Can anybody help me with this? What should I do to allow myself to retrieve tweets from Twitter?
This is my current code (the keys and tokens were replaced as 'XXXX'):
import tweepy
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_token = 'XXXX'
access_token_secret = 'XXXX'
client = tweepy.Client(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret
)
query = 'QUERY'
tweets = client.search_recent_tweets(query=query, max_results=10)
for tweet in tweets.data:
print(tweet.text)
Error:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\Year 3 (Sem 1)\Investigation Report\Coding\Testing Scrape 5.py", line 17, in <module>
tweets = client.search_recent_tweets(query=query, max_results=10)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\Desktop\Year 3 (Sem 1)\Investigation Report\Coding\venv\Lib\site-packages\tweepy\client.py", line 1266, in search_recent_tweets
return self._make_request(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\Desktop\Year 3 (Sem 1)\Investigation Report\Coding\venv\Lib\site-packages\tweepy\client.py", line 129, in _make_request
response = self.request(method, route, params=request_params,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\Desktop\Year 3 (Sem 1)\Investigation Report\Coding\venv\Lib\site-packages\tweepy\client.py", line 98, in request
raise Unauthorized(response)
tweepy.errors.Unauthorized: 401 Unauthorized
Unauthorized
First of all you should have been subscribed to basic plan of Twitter. Also use twitter V2 APIs use reference:
https://github.com/twitterdev/Twitter-API-v2-sample-code/tree/main
.I also fixed the issue by following github code in recent.
Also you can check postman collection:
https://www.postman.com/twitter/workspace/twitter-s-public-workspace/