My code is as simple as this, but it gave me 403 error:
def init_api():
api = json.load(open('secret.json'))['twitterapi']
consumer_key = api['api_token']
consumer_secret = api['api_secret']
access_token = api["access_token"]
access_token_secret = api["access_secret"]
bearer_token = api["bearer_token"]
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#Step 2 - Retrieve Tweets
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)
if __name__ == "__main__":
init_api()
Traceback (most recent call last):
File "path/to/project/config.py", line 45, in <module>
init_api()
File "path/to/project/config.py", line 39, in init_api
public_tweets = api.home_timeline()
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 33, in wrapper
return method(*args, **kwargs)
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 488, in home_timeline
return self.request(
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 259, in request
raise Forbidden(resp)
tweepy.errors.Forbidden: 403 Forbidden
453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
(venv)
2
Answers
From the error message:
So, you are only allowed to use v2 endpoints, but
api.home_timeline()
is a v1.1 API, so the v2 endpoint for reading the timeline is:Or, since you are using
tweepy
, use their v2 APIRequest the elevated v1.1 access. It generally gets accepted either instantly or very quickly. May not solve your issue though, because since they folded StreamListener into Stream, I can’t seem to figure it out myself.