Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 状态网,urllib.error.HTTPError:HTTP错误401:未经授权_Python_Api_Urllib - Fatal编程技术网

Python 状态网,urllib.error.HTTPError:HTTP错误401:未经授权

Python 状态网,urllib.error.HTTPError:HTTP错误401:未经授权,python,api,urllib,Python,Api,Urllib,这是我的代码: #!/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

这是我的代码:

#!/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

我的代码可能有什么问题?

如果您可以使用urllib2以外的内容,我强烈建议您使用此库:非常有用,谢谢!