Python Twitter Urllib2.httperror Http Error 401 Unauthorized
Contents |
here for a quick overview of the site Help Center Detailed answers python http error 401: unauthorized to any questions you might have Meta Discuss the workings
Python Urllib.error.httperror Http Error 401 Unauthorized
and policies of this site About Us Learn more about Stack Overflow the company Business
Python 3 Urllib.error.httperror Http Error 401 Unauthorized
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Python3 Urllib.error.httperror: Http Error 401: Unauthorized
Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python: urllib2.HTTPError: HTTP Error 401: Unauthorized up vote 6 down vote favorite 2 I was trying to load a web page, but I ran httppasswordmgrwithdefaultrealm into this problem. I do have the username and password, but I don't know how to use them in python code. I looked up on python tutorial, and this is what I wrote: import urllib2 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() username = 'user' password = 'pass' top_level_url = "www.something.com:80" password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) opener.open('http://www.something.com/h/h.html') urllib2.install_opener(opener) response = urllib2.urlopen() page = response.read() print page Anything wrong? python authentication share|improve this question edited Apr 17 '12 at 17:44 Rup 22.7k64977 asked Apr 17 '12 at 17:18 stupidguy 136128 add a comment| 2 Answers 2 active oldest votes up vote 16 down vote Here is working code import urllib2 url = 'http://www.abc.com/index.html' username = 'user' password = 'pass' p = urllib2.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, url, username, password) handler = urllib2.HTTPBasicAuthHandler(p) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) page = urllib2.urlopen(url).read() share|improve this answer answered Jun 22 '12 at 18:48 big 54521429
here for a quick overview of the site Help Center Detailed answers to any questions you might python sharepoint unauthorized have Meta Discuss the workings and policies of this site About python urllib2 authentication example Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads python requests module with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, http://stackoverflow.com/questions/10195915/python-urllib2-httperror-http-error-401-unauthorized just like you, helping each other. Join them; it only takes a minute: Sign up Status net, urllib.error.HTTPError: HTTP Error 401: Unauthorized up vote 1 down vote favorite This is my code : #!/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 = http://stackoverflow.com/questions/18809675/status-net-urllib-error-httperror-http-error-401-unauthorized 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() When I execute this code, I get an error: 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 What maybe wrong with my code? python api urllib share|improve this question asked Sep 15 '13 at 6:46 Rayeshman 133 I highly recommend this library if it's possible for your to use something other than urllib2: docs.python-requests.org/en/latest –John Sheehan - Runscope Sep 15 '13 at 6:51 Really helpful ,Thank you! –Rayeshman Sep 15 '13 at 7:36 add a comment| active oldest votes Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook. Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email dis
revision of this HOWTO, available at urllib2 - Le Manuel manquant. Introduction¶ Related Articles You may also find useful the following article on fetching web resources with Python: Basic Authentication A tutorial on Basic https://docs.python.org/3/howto/urllib2.html Authentication, with examples in Python. urllib.request is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. These are provided by objects called handlers and http error openers. urllib.request supports fetching URLs for many "URL schemes" (identified by the string before the ":" in URL - for example "ftp" is the URL scheme of "ftp://python.org/") using their associated network protocols (e.g. FTP, HTTP). This tutorial focuses on the most common case, HTTP. For straightforward situations urlopen is very easy to use. But as soon as you encounter errors or non-trivial cases when opening HTTP URLs, you http error 401 will need some understanding of the HyperText Transfer Protocol. The most comprehensive and authoritative reference to HTTP is RFC 2616. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using urllib, with enough detail about HTTP to help you through. It is not intended to replace the urllib.request docs, but is supplementary to them. Fetching URLs¶ The simplest way to use urllib.request is as follows: import urllib.request with urllib.request.urlopen('http://python.org/') as response: html = response.read() If you wish to retrieve a resource via URL and store it in a temporary location, you can do so via the urlretrieve() function: import urllib.request local_filename, headers = urllib.request.urlretrieve('http://python.org/') html = open(local_filename) Many uses of urllib will be that simple (note that instead of an ‘http:' URL we could have used a URL starting with ‘ftp:', ‘file:', etc.). However, it's the purpose of this tutorial to explain the more complicated cases, concentrating on HTTP. HTTP is based on requests and responses - the client makes requests and servers send responses. urllib.request mirrors this with a Request object which represents the HTTP request you are making. In its simplest form you create a Request object that specifies the URL you wan