2012-02-28 15 views
4

我有相同一段編碼處理Twitter用戶流運行在兩臺不同的機器。這兩款機器都是使用python 2.6.5的Ubuntu Lucid,但在我家的機器上,我收到了HTTP Error 401: Unauthorized,而在大學時,它完美地工作。在兩臺機器上,當我使用curl和相同的參數(即消費者密鑰,消費者機密,訪問令牌和訪問密鑰)時,它完美地工作。在Python中使用OAuth的Twitter流在兩臺同等配置的機器上表現不同

看到代碼波紋管,它是由Josh Sharp

from oauth.oauth import OAuthRequest, OAuthSignatureMethod_HMAC_SHA1 
from hashlib import md5 
import json, time 
import random, math, re, urllib, urllib2 

STREAM_URL = "https://userstream.twitter.com/2/user.json" 

class Token(object): 
    def __init__(self,key,secret): 
     self.key = key 
     self.secret = secret 

    def _generate_nonce(self): 
     random_number = ''.join(str(random.randint(0, 9)) for i in range(40)) 
     m = md5(str(time.time()) + str(random_number)) 
     return m.hexdigest() 

CONSUMER_KEY = 'consumer_key' 
CONSUMER_SECRET = 'consumer_secret' 
ACCESS_TOKEN = 'token' 
ACCESS_TOKEN_SECRET = 'token_secret' 

access_token = Token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET) 
consumer = Token(CONSUMER_KEY,CONSUMER_SECRET) 

parameters = { 
    'oauth_consumer_key': CONSUMER_KEY, 
    'oauth_token': access_token.key, 
    'oauth_signature_method': 'HMAC-SHA1', 
    'oauth_timestamp': str(int(time.time())), 
    'oauth_nonce': access_token._generate_nonce(), 
    'oauth_version': '1.0', 
} 


oauth_request = OAuthRequest.from_token_and_callback(access_token, 
       http_url=STREAM_URL, 
       parameters=parameters) 
signature_method = OAuthSignatureMethod_HMAC_SHA1() 
signature = signature_method.build_signature(oauth_request, consumer, access_token) 

parameters['oauth_signature'] = signature 

data = urllib.urlencode(parameters) 

req = urllib2.urlopen("%s?%s" % (STREAM_URL,data)) 
buffer = '' 


# We're using urllib2 to avoid external dependencies 
# even though pyCurl actually handles the callbacks 
# much more gracefully than this clumsy method. 
# We read a byte at a time until we find a newline 
# which indicates the end of a chunk. 

while True: 

    chunk = req.read(1) 
    if not chunk: 
     print buffer 
     break 

    chunk = unicode(chunk) 
    buffer += chunk 

    tweets = buffer.split("\n",1) 
    if len(tweets) > 1: 
     print tweets[0] 
     buffer = tweets[1] 

誤差產生時,我嘗試在國內執行的是:

File "py_stream.py", line 48, in <module> 
req = urllib2.urlopen("%s?%s" % (STREAM_URL,data)) 
File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
File "/usr/lib/python2.6/urllib2.py", line 397, in open 
    response = meth(req, response) 
File "/usr/lib/python2.6/urllib2.py", line 510, in http_response 
    'http', request, response, code, msg, hdrs) 
File "/usr/lib/python2.6/urllib2.py", line 435, in error 
    return self._call_chain(*args) 
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain 
    result = func(*args) 
File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 401: Unauthorized 

由於它的工作原理用在兩臺機器上嫋嫋,我想沒有什麼是與SSL認證有關的錯誤。但同時它讓我想知道當我在家中使用它時會發生什麼失敗。

回答

6

經過數週的努力找出問題所在,我發現時鐘與負責Twitter流的時鐘並沒有很好的同步。因此,Twitter返回401:未經授權。

如果您正在使用Ubuntu,就可以解決因此使用ntpdate如下這個問題:

sudo ntpdate ntp.ubuntu.com 
相關問題