Search code examples
python-3.xooptweepytwitter-oauthtwitterapi-python

TypeError: __init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'


I am trying to fetch the tweets from Twitter via tweepy API and for that, I have written the following code consisting of sockets for connecting to the pyspark for further analysis of the tweets that are being fetched. Code:

import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
import socket
import json

consumer_key="IvWeydhuRZoHbigNvX9wl8FbX"
consumer_secret="hv9NnWV64f4ECnK3eqJIhwQiu0SoDMtL1b9ImPJJFPwSSKleOg"
access_token="3945967939-cqy1y3J6cPcKi2fohK3qr5QSB2Z3lrOcs863tPI"
access_secret="oJIXDC37NsRU1FNP4jznCyIVwRiBjaHF9L3qsyxybkykS"

class TweetListener(Stream):
    def __init__(self, csocket):
        self.client_socket=csocket
    def on_data(self, data):
        try:
            msg = json.loads(data)
            print(msg('text').encode('utf-8'))
            self.client_socket.send(msg('text').encode('utf-8'))
            return True
        except BaseException as e:
            print('Error %s'%str(e))
        return True
    def on_error(self, status):
        print(status)
        return True

def send_data(c_socket):
    auth = OAuthHandler(consumer_key,consumer_secret)
    auth.set_access_token(access_token,access_secret)
    twtr_stream = Stream(auth, TweetListener(c_socket))
    twtr_stream.filter(track=['ETH'])

s = socket.socket()
host = "127.0.0.1"
port = 5000
s.bind((host,port))
print("Active port %s"%str(port))

send_data(c)

Error in the ouput:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_22860/3758390964.py in <module>
----> 1 send_data(c)

/tmp/ipykernel_22860/1165989285.py in send_data(c_socket)
      2     auth = OAuthHandler(consumer_key,consumer_secret)
      3     auth.set_access_token(access_token,access_secret)
----> 4     twtr_stream = Stream(auth, TweetListener(c_socket))
      5     twtr_stream.filter(track=['ETH'])

TypeError: __init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'

Solution

  • This looks completely contrary to how the Stream class is supposed to be used.

    The API keys are supposed to be passed as arguments to the Stream base class constructor. There is no need to additionally construct another Stream object, or deal with OAuthHandler at all.

    The quickest way to patch this would be:

    class TweetListener(Stream):
        def __init__(self, *args, csocket):
            super().__init__(*args)
            self.client_socket = csocket
        ...
    
    ...
    
    def send_data(c_socket):
        twtr_stream = TweetListener(
            consumer_key, consumer_secret,
            access_token, access_secret,
            csocket=c_socket
        )
        twtr_stream.filter(track=['ETH'])
    

    As an aside, I would expect those API keys to be revoked pretty quickly.