pycurl - python throwing an error HTTP 401 while accessing https://stream.twitter.com/1.1/statuses/filter.json -


this code running stream of tweets using streaming api accessing stream.twitter url mentioned in title. throwing error (http error 401) in code trying track multiple terms

import time import pycurl import urllib import json import oauth2  api_endpoint_url = 'https://stream.twitter.com/1.1/statuses/filter.json' user_agent = 'twitterstream 1.0' oauth_keys = {'consumer_key': 'abc',               'consumer_secret': 'abc',               'access_token_key': 'abc',               'access_token_secret': 'abc'} post_params = {'include_entities': 0,                'stall_warning': 'true',                'track': 'iphone,ipad,ipod'} class twitterstream():     def __init__(self, timeout=false):         #self.oauth_token = token(key=oauth_keys['access_token_key'], secret=oauth_keys['access_token_secret'])         self.oauth_consumer = oauth2.consumer(key=oauth_keys['consumer_key'], secret=oauth_keys['consumer_secret'])         self.oauth_token = oauth2.token(key=oauth_keys['access_token_key'], secret=oauth_keys['access_token_secret'])         self.conn = none #pycurl.curl()         self.buffer = ''         self.timeout = timeout         self.setup_connection()      def setup_connection(self):         """ create persistant http connection streaming api endpoint using curl.         """         if self.conn:             self.conn.close()             self.buffer = ''         self.conn = pycurl.curl()         # restart connection if less 1 byte/s received during "timeout" seconds         if isinstance(self.timeout, int):             self.conn.setopt(pycurl.low_speed_limit, 1)             self.conn.setopt(pycurl.low_speed_time, self.timeout)         self.conn.setopt(pycurl.url, api_endpoint_url)         self.conn.setopt(pycurl.useragent, user_agent)         # using gzip optional saves bandwidth.         self.conn.setopt(pycurl.encoding, 'deflate, gzip')         self.conn.setopt(pycurl.post, 1)         self.conn.setopt(pycurl.postfields, urllib.urlencode(post_params))         self.conn.setopt(pycurl.httpheader, ['host: stream.twitter.com',                                              'authorization: %s' % self.get_oauth_header()])         # self.handle_tweet method called when new tweets arrive         self.conn.setopt(pycurl.writefunction, self.handle_tweet)      def get_oauth_header(self):         """ create , return oauth header.         """         params = {'oauth2_version': '1.0',                   'oauth2_nonce': oauth2.generate_nonce(),                   'oauth2_timestamp': int(time.time())}         req = oauth2.request(method='post', parameters=params, url='%s?%s' % (api_endpoint_url,                                                                              urllib.urlencode(post_params)))         req.sign_request(oauth2.signaturemethod_hmac_sha1(), self.oauth_consumer, self.oauth_token)         return req.to_header()['authorization'].encode('utf-8')      def start(self):         """ start listening streaming endpoint.         handle exceptions according twitter's recommendations.         """         backoff_network_error = 0.25         backoff_http_error = 5         backoff_rate_limit = 60         while true:             self.setup_connection()             try:                 self.conn.perform()             except:                 # network error, use linear off 16 seconds                 print 'network error: %s' % self.conn.errstr()                 print 'waiting %s seconds before trying again' % backoff_network_error                 time.sleep(backoff_network_error)                 backoff_network_error = min(backoff_network_error + 1, 16)                 continue             # http error             sc = self.conn.getinfo(pycurl.http_code)             if sc == 420:                 # rate limit, use exponential off starting 1 minute , double each attempt                 print 'rate limit, waiting %s seconds' % backoff_rate_limit                 time.sleep(backoff_rate_limit)                 backoff_rate_limit *= 2             else:                 # http error, use exponential off 320 seconds                 print 'http error %s, %s' % (sc, self.conn.errstr())                 print 'waiting %s seconds' % backoff_http_error                 time.sleep(backoff_http_error)                 backoff_http_error = min(backoff_http_error * 2, 320)      def handle_tweet(self, data):         """ method called when data received through streaming endpoint.         """         self.buffer += data         if data.endswith('\r\n') , self.buffer.strip():             # complete message received             message = json.loads(self.buffer)             self.buffer = ''             msg = ''             if message.get('limit'):                 print 'rate limiting caused miss %s tweets' % (message['limit'].get('track'))             elif message.get('disconnect'):                 raise exception('got disconnect: %s' % message['disconnect'].get('reason'))             elif message.get('warning'):                 print 'got warning: %s' % message['warning'].get('message')             else:                 print 'got tweet text: %s' % message.get('text')   if __name__ == '__main__':     ts = twitterstream()     ts.setup_connection()     ts.start() 

please me resolve it.

you try updating clock.

sudo ntpdate ntp.ubuntu.com 

from: http://lembra.wordpress.com/2012/03/08/twitter-stream-mysterious-401unauthorized-status-with-oauth-and-clock-issue/


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -