这是我的代码:
#!/usr/bin/python3
import urllib.request
import urllib.parse
def send_to_statusnet():
msg = ":)"
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("T2S",
"http://quitter.se/api/", "USER", "PASS")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( {'status': msg} )
params = params.encode('utf-8')
resp = urllib.request.urlopen("http://quitter.se/api/statuses/update.json", params)
resp.read()
send_to_statusnet()
当我执行这段代码时,我得到一个错误:
File "/usr/lib64/python3.3/urllib/request.py", line 595, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
我的代码可能有什么问题?