【问题标题】:Tweepy error when using class for OAuthHandler: AttributeError: Authentication instance has no attribute 'apply_auth'使用 OAuthHandler 类时出现 Tweepy 错误:AttributeError:身份验证实例没有属性“apply_auth”
【发布时间】:2018-11-19 18:10:57
【问题描述】:

我是 python 新手,我正在使用 tweepy 库进行 twitter 流式传输,我正在尝试创建一个类来为我处理 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

我有另一个实现 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

问题是当我尝试访问主类上的 myStream 方法时,例如 My Stream Filter,我收到以下错误:

# 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")

Traceback(最近一次通话最后一次): 文件“C:/Users/Sosa9/PycharmProjects/bigDataTweetListener/main.py”,第 17 行,在 myStream.filter(track="世界杯") 过滤器中的文件“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() 文件“C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py”,第 105 行,在 _run self.auth.apply_auth(url, 'POST', self.headers, self.parameters) AttributeError:身份验证实例没有属性“apply_auth”

如果我已授予创建身份验证类实例的访问权限,我不知道为什么无法访问流类中的 methods.attributes。有什么想法吗?

【问题讨论】:

    标签: python python-2.7 tweepy


    【解决方案1】:

    首先,你是 python2 和 3 的混合。你应该:“打印(状态)”,而不是“打印状态”来使用 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() 返回auth,所以替换这个:

    auth.getAuthorization()
    

    作者:

    auth = auth.getAuthorization()
    

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      相关资源
      最近更新 更多