'Tweepy error Stream.__init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'

I'm trying to set up a Retweet bot using the Tweepy module for Twitter's API, and have been working through errors bit by bit as the tutorial I was following was an older version of Tweepy. I'm new-ish to Python coding, and certainly haven't integrated API's before, so bear with me on this :)

Here's my code:

# import dependencies
import tweepy
import os
from dotenv import load_dotenv
from streamlistener import StreamListener

# load our .env file to make use of the environment variables
load_dotenv()

# import and assign our environment variables
API_KEY = os.getenv('twitter_api_key')
API_SECRET = os.getenv('twitter_api_secret')
ACCESS_TOKEN = os.getenv('twitter_access_token')
TOKEN_SECRET = os.getenv('twitter_access_secret')

# instantiate oauth handler and set access token
twitter_oauth = tweepy.OAuthHandler(API_KEY, API_SECRET)
twitter_oauth.set_access_token(ACCESS_TOKEN, TOKEN_SECRET)

# instantiate tweepy api object using the authentication handler object
twitter_api = tweepy.API(twitter_oauth)

# attempt credential verification. prints exception if something is wrong
try:
    print(twitter_api.verify_credentials())
    print("Successfully logged in")
except tweepy.errors.TweepyException as e:
    print(e)
except Exception as e:
    print(e)
    
# instantiate a StreamListener object
tweets_listener = StreamListener(twitter_api)

#instantiate a tweepy.Stream object
tweet_stream = tweepy.Stream(twitter_api.auth, tweets_listener)

# Use the filter method
tweet_stream.filter(track=["#VTuber", "#ENVTuber", "vtuber", "Vtuber", "VTuberEN"], languages=["en"])

And the error I'm getting is:

Traceback (most recent call last):   File "C:\Python\bot.py", line 36, in <module>     tweet_stream = tweepy.Stream(twitter_api.auth, tweets_listener) TypeError: Stream.__init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'

I've tried adding ACCESS_TOKEN and TOKEN_SECRET into line 36 (tweepy.Stream arguments), but either I did it wrong or that's not the fix.



Solution 1:[1]

You're using syntax from an older version of Tweepy.
Tweepy v4.0.0 changed Stream to accept consumer_key, consumer_secret, access_token, and access_token_secret parameters for initialization.

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 Harmon758