2016-12-07 104 views
0

爲什麼此代碼會不斷返回401錯誤的任何原因?我從這裏克隆它「https://github.com/purplewove/social-media-apis」來測試twython的功能,但儘管我有我的twitter密鑰和祕密,但我只獲得了很多401的信息,而不是實際的tweets。Twython 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) 
+0

你確定你的應用沒有被暫停或限制在https://apps.twitter.com? –

回答

0

我再生的鑰匙和它的工作。每次更換機器時都需要執行此操作。不知道是什麼問題...