【问题标题】:Twython 401 errorTwython 401 错误
【发布时间】:2016-12-07 07:53:02
【问题描述】:

为什么这段代码会不断返回 401 错误?我从这里“https://github.com/purplewove/social-media-apis”克隆了它以测试 twython 功能,但虽然我有我的 Twitter 密钥和秘密,但我只得到了很多 401,而不是实际的推文。

我习惯了 Python 3.5,但这似乎在 3.X 或 2.X 中运行时没有任何客户端错误。我正在使用 Anaconda 包,因为“pip install twython”已成功。

代码如下(源代码不变):

'''
social-media-apis
=================

currently just a bit of code for keyword searches using twython

to use, make sure twython is installed and then type: 
python filter_terms.py [consumer_key] [consumer_secret] [access_token_key] [access_token_secret] [search_term]

and the script will dump the text from all tweets gleaned from the streaming api containing the search term to standard output. 

find consumer_key, consumer_secret, access_token_key, access_token_secret here: dev.twitter.com/apps
'''

#filter_terms.py

from Stream import Stream
import sys


app_key = sys.argv[1]
app_secret = sys.argv[2]
oauth_token = sys.argv[3]
oauth_token_secret = sys.argv[4]
terms = sys.argv[5:]


stream = Stream(app_key, app_secret, oauth_token, oauth_token_secret)
stream.filter_terms(terms)


#Stream.py

from MyStreamer import MyStreamer

class Stream():
    def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret):
        self.streamer = MyStreamer(app_key, app_secret, oauth_token, oauth_token_secret)

    def filter_terms(self, terms):
        self.streamer.filter_terms(terms)    

#MyStreamer.py

from twython import TwythonStreamer
import sqlite3
import sys
import datetime

class MyStreamer(TwythonStreamer):
    '''a simple streamer that prints the text of a tweet to standard output'''
    def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret):
      TwythonStreamer.__init__(self, app_key, app_secret, oauth_token, oauth_token_secret)

    def on_success(self, data):
        if 'text' in data:
            print (data['text'])

    def on_error(self, status_code, data):
        print(status_code)

    def filter_terms(self, terms):
        '.'.join(terms)
        self.statuses.filter(track = terms)

【问题讨论】:

标签: python twython


【解决方案1】:

我重新生成了密钥并且它起作用了。每次更换机器时都需要这样做。不知道是什么问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2016-11-24
    • 2013-06-30
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多