Head First Programming:示例程序出错

时间:2011-06-27 01:09:41

标签: python

我已经完成了Head First Programming第3章中概述的程序。 基本上,该程序搜索网站并将价格存储在该页面上。然后,根据用户选择的选项,某个消息将被发送到用户的Twitter帐户。

图书网站上的源代码:http://headfirstlabs.com/books/hfprog/chapter03/page108.py

当我运行我的程序,并从本书的网站运行源代码时,我得到同样的错误。

这是错误:

Traceback (most recent call last):
  File "C:\Users\Krysten\Desktop\Ch3.py", line 28, in <module>
    send_to_twitter(get_price())
  File "C:\Users\Krysten\Desktop\Ch3.py", line 14, in send_to_twitter
    resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
  File "C:\Python31\lib\urllib\request.py", line 121, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python31\lib\urllib\request.py", line 356, in open
    response = meth(req, response)
  File "C:\Python31\lib\urllib\request.py", line 468, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python31\lib\urllib\request.py", line 394, in error
    return self._call_chain(*args)
  File "C:\Python31\lib\urllib\request.py", line 328, in _call_chain
    result = func(*args)
  File "C:\Python31\lib\urllib\request.py", line 476, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

是否导致错误,因为该书有些过时,必须以不同的方式访问Twitter?

1 个答案:

答案 0 :(得分:2)

在大多数Twitter API中,不推荐使用基本身份验证。使用OAuth API。