首页 文章

使用OAuthHandler类时出现错误:AttributeError:身份验证实例没有属性'apply_auth'

提问于
浏览
2

我是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 Streaming的类

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

回溯(最近一次调用最后一次):文件"C:/Users/Sosa9/PycharmProjects/bigDataTweetListener/main.py",第17行,在myStream.filter(track = "world cup")文件"C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py",第228行,在过滤器self._start(async)文件"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是不可访问的 . 有任何想法吗?

1 回答

  • 0

    首先,你是mixin python2和3.你应该:“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() 返回 auth ,所以替换为:

    auth.getAuthorization()
    

    通过:

    auth = auth.getAuthorization()
    

相关问题