'Why I get forbidden error for only reading home timeline?

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)

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​



Solution 1:[1]

From the error message:

453 - You currently have Essential access which includes access to Twitter API v2 endpoints only.

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:

GET /2/users/:id/tweets

Or, since you are using tweepy, use their v2 API

Client.get_users_tweets(id, *, end_time=None, exclude=None, expansions=None, max_results=None,
            media_fields=None, pagination_token=None, place_fields=None, poll_fields=None,
            since_id=None, start_time=None, tweet_fields=None, until_id=None,
            user_fields=None, user_auth=False)

Solution 2:[2]

Request 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ken Y-N
Solution 2 gordonzed