《头号编程:示例程序中的错误》

0 投票
1 回答
641 浏览
提问于 2025-04-16 20:20

我完成了《Head First Programming》第三章中提到的一个程序。这个程序的基本功能是搜索一个网站,并把那个页面上的价格存储起来。然后,根据用户选择的选项,会向用户的推特账户发送特定的消息。

书中代码的来源网址是: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

这个错误是因为书里的内容有点过时,而推特的访问方式发生了变化吗?

1 个回答

2

在大多数Twitter的API中,基本的身份验证已经不再使用了。现在要用OAuth这种方式来进行身份验证。

撰写回答