0

我已经安装了 Python 2.7 并尝试使用 Twitter 包。我已成功安装了 3 个必备软件包...我已获得使用者密钥和访问令牌...但不知何故收到此错误:

>>> import twitter
>>> api = twitter.Api(consumer_key='consumer_key',
                  consumer_secret='consumer_secret',
                  access_token_key='access_token',
                  access_token_secret='access_token_secret')

 >>> print api.VerifyCredentials()

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
print api.VerifyCredentials()
File "build\bdist.win32\egg\twitter.py", line 4257, in VerifyCredentials
json = self._FetchUrl(url, no_cache=True)
File "build\bdist.win32\egg\twitter.py", line 4611, in _FetchUrl
response = opener.open(url, encoded_post_data)
File "C:\Python\Python27\lib\urllib2.py", line 404, in open
response = self._open(req, data)
File "C:\Python\Python27\lib\urllib2.py", line 422, in _open
'_open', req)
File "C:\Python\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python\Python27\lib\urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "C:\Python\Python27\lib\urllib2.py", line 1184, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

所有其他命令也导致相同的错误......例如

statuses = api.GetUserTimeline(user)

任何帮助,将不胜感激...

4

1 回答 1

0

首先,你连接到互联网了吗?从终端您可以尝试:

ping www.google.com

其次,您是否将“consumer_key”替换为实际值,即“1234ABCD”?

api = twitter.Api(consumer_key='ABCD1234', consumer_secret='1234ABCD', access_token_key='DCBA4321', access_token_secret='4321DCBA')

另请注意,您传递的值必须是字符串,因此请在其周围加上引号。而且对于 python 中的多行,在每个不完整的行之后都有一个反斜杠:

api = twitter.Api(consumer_key='ABCD1234', \
              consumer_secret='1234ABCD', \
              access_token_key='DCBA4321', \
              access_token_secret='4321DCBA')
于 2013-09-30T09:12:44.143 回答