Tweepy error when using class for OAuthHandler: AttributeError: Authentication instance has no attribute 'apply_auth'
Tweepy error when using class for OAuthHandler: AttributeError: Authentication instance has no attribute 'apply_auth'
我是 python 的新手,我正在使用 tweepy 库进行 Twitter 流式传输,我正在尝试创建一个 class 来为我处理 OAth 身份验证。
from config.config import consumer_secret, consumer_key, secret, token
import tweepy
from tweepy.auth import OAuthHandler
from tweepy import API
from tweepy import OAuthHandler
class Authentication:
"""
Class that handles the authentication for streaming
http://docs.tweepy.org/en/v3.5.0/auth_tutorial.html
"""
def __init__(self):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.secret = secret
self.token = token
def getAuthorization(self):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.secret = secret
self.token = token
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.token, self.secret)
return auth
我有另一个 class 实现了 Twitter 流
import tweepy
from tweepy.streaming import StreamListener
from tweepy import Stream
from tweepy.auth import OAuthHandler
class Listener(tweepy.StreamListener):
"""
Class that inherits from tweepy StreamListener
http://tweepy.readthedocs.io/en/v3.5.0
"""
def on_status(self, status):
print status
print(status.text)
def on_data(self, data):
print data
return True
def on_error(self, status):
if status == 420:
# returning False in on_data disconnects the stream
print status
return False
问题是当我尝试访问主要 class 上的 myStream 方法时,例如我的流过滤器,我得到以下错误:
# main class for the bigDataTweetListener project
import tweepy
from tweepy import Stream
from lib.authentication import Authentication
from lib.tweetListener import Listener
from tweepy.auth import OAuthHandler
# API Authentication
auth = Authentication()
auth.getAuthorization()
api = tweepy.API(auth)
print("authorization successful")
#Listen a Stream by topic
myStreamListener = Listener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(track="world cup")
回溯(最近调用最后):
文件 "C:/Users/Sosa9/PycharmProjects/bigDataTweetListener/main.py",第 17 行,位于
myStream.filter(曲目="world cup")
文件 "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第 228 行,在过滤器中
self._start(异步)
文件 "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第 172 行,在 _start
self._run()
_run 中的文件 "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第 105 行
self.auth.apply_auth(url, 'POST', self.headers, self.parameters)
AttributeError:身份验证实例没有属性 'apply_auth'
我不知道为什么如果我已授予创建身份验证实例 class 的访问权限,则无法访问来自流 class 的 methods.attributes。有什么想法吗?
首先,你混合了 python2 和 3。你应该:"print(status)",而不是 "print status" 使用 Python3。 (也打印(数据))。
其次,getAuthorization()
中的前四行没有用:您已经在 __init__
中声明了那些变量。所以你可以删除或评论它们:
def getAuthorization(self):
# self.consumer_key = consumer_key
# self.consumer_secret = consumer_secret
# self.secret = secret
# self.token = token
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.token, self.secret)
return auth
最后,getAuthorization()
returns auth
,所以替换为:
auth.getAuthorization()
作者:
auth = auth.getAuthorization()
我是 python 的新手,我正在使用 tweepy 库进行 Twitter 流式传输,我正在尝试创建一个 class 来为我处理 OAth 身份验证。
from config.config import consumer_secret, consumer_key, secret, token
import tweepy
from tweepy.auth import OAuthHandler
from tweepy import API
from tweepy import OAuthHandler
class Authentication:
"""
Class that handles the authentication for streaming
http://docs.tweepy.org/en/v3.5.0/auth_tutorial.html
"""
def __init__(self):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.secret = secret
self.token = token
def getAuthorization(self):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.secret = secret
self.token = token
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.token, self.secret)
return auth
我有另一个 class 实现了 Twitter 流
import tweepy
from tweepy.streaming import StreamListener
from tweepy import Stream
from tweepy.auth import OAuthHandler
class Listener(tweepy.StreamListener):
"""
Class that inherits from tweepy StreamListener
http://tweepy.readthedocs.io/en/v3.5.0
"""
def on_status(self, status):
print status
print(status.text)
def on_data(self, data):
print data
return True
def on_error(self, status):
if status == 420:
# returning False in on_data disconnects the stream
print status
return False
问题是当我尝试访问主要 class 上的 myStream 方法时,例如我的流过滤器,我得到以下错误:
# main class for the bigDataTweetListener project
import tweepy
from tweepy import Stream
from lib.authentication import Authentication
from lib.tweetListener import Listener
from tweepy.auth import OAuthHandler
# API Authentication
auth = Authentication()
auth.getAuthorization()
api = tweepy.API(auth)
print("authorization successful")
#Listen a Stream by topic
myStreamListener = Listener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(track="world cup")
回溯(最近调用最后): 文件 "C:/Users/Sosa9/PycharmProjects/bigDataTweetListener/main.py",第 17 行,位于 myStream.filter(曲目="world cup") 文件 "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第 228 行,在过滤器中 self._start(异步) 文件 "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第 172 行,在 _start self._run() _run 中的文件 "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第 105 行 self.auth.apply_auth(url, 'POST', self.headers, self.parameters) AttributeError:身份验证实例没有属性 'apply_auth'
我不知道为什么如果我已授予创建身份验证实例 class 的访问权限,则无法访问来自流 class 的 methods.attributes。有什么想法吗?
首先,你混合了 python2 和 3。你应该:"print(status)",而不是 "print status" 使用 Python3。 (也打印(数据))。
其次,getAuthorization()
中的前四行没有用:您已经在 __init__
中声明了那些变量。所以你可以删除或评论它们:
def getAuthorization(self):
# self.consumer_key = consumer_key
# self.consumer_secret = consumer_secret
# self.secret = secret
# self.token = token
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.token, self.secret)
return auth
最后,getAuthorization()
returns auth
,所以替换为:
auth.getAuthorization()
作者:
auth = auth.getAuthorization()