2016-07-27 147 views
0

我想在Python和MongoDB之間設置一個連接來存儲推文。我的問題是,我在Mylistener的每個請求中獲得401。你能幫我解決這個問題嗎?401錯誤python MongoDB連接

class MyListener(StreamListener): 
    def __init__(self, start_time, time_limit=60): 
     self.time = start_time 
     self.limit = time_limit 
     self.tweet_data = [] 
    def on_data(self, data): 
     while (time.time() - self.time)<self.limit: 
       try: 
       client = MongoClient('localhost',27017) 
       client.server_info() 
       db = client.tweets_db 
       collection = db.collect_tweets 
       tweet = json.loads(data) 
       collect_tweets.insert(tweet) 
       return True 
      except BaseException as e: 
       print("Error on_data: %s" % str(e)) 
      return True 
     def on_error(self, status): 
     print(status) 
Keywords_list=['word1','word2','word3'] 
twitter_stream = Stream(auth, MyListener(start_time, time_limit)) 
+0

401見here代表URL受密碼保護。 –

回答

0

聽起來像是你需要創建新MongoClient實例後立即與數據庫進行驗證:

client.tweets_db.authenticate('user', 'password') 

更詳細的例子

+0

感謝您的回答,實際上問題是與twitter API沒有mongodb連接,我不得不重置系統時鐘,它工作得很好。 – user2804064