Python Urllib2.httperror Http Error 401 Unauthorized
Contents |
here for a quick overview of the site Help Center python 3 urllib.error.httperror http error 401 unauthorized Detailed answers to any questions you might have Meta python3 urllib.error.httperror: http error 401: unauthorized Discuss the workings and policies of this site About Us Learn more about Stack Overflow httppasswordmgrwithdefaultrealm the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Typeerror: 'bytes' Object Is Not Callable
Dismiss Join the Stack 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 urllib python 3 a web page, but I ran 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(
here for a quick overview of the site Help Center Detailed answers to any questions you might have python urllib3 Meta Discuss the workings and policies of this site About Us Learn
Urllib Vs Urllib2
more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us
Importerror: No Module Named Request
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, just like http://stackoverflow.com/questions/10195915/python-urllib2-httperror-http-error-401-unauthorized you, helping each other. Join them; it only takes a minute: Sign up “HTTP Error 401: Unauthorized” when querying youtube api for playlist with python up vote 1 down vote favorite 2 I try to write a simple python3 script that gets some playlist informations via the youtube API. However I always get a 401 Error http://stackoverflow.com/questions/26451173/http-error-401-unauthorized-when-querying-youtube-api-for-playlist-with-pytho whereas it works perfectly when I enter the request string in a browser or making a request with w-get. I'm relatively new to python and I guess I'm missing some important point here. This is my script. Of course I actually use a real API-Key. from urllib.request import Request, urlopen from urllib.parse import urlencode api_key = "myApiKey" playlist_id = input('Enter playlist id: ') output_file = input('Enter name of output file (default is playlist id') if output_file == '': output_file = playlist_id url = 'https://www.googleapis.com/youtube/v3/playlistItems' params = {'part': 'snippet', 'playlistId': playlist_id, 'key': api_key, 'fields': 'items/snippet(title,description,position,resourceId/videoId),nextPageToken,pageInfo/totalResults', 'maxResults': 50, 'pageToken': '', } data = urlencode(params) request = Request(url, data.encode('utf-8')) response = urlopen(request) content = response.read() print(content) Unfortunately it rises a error at response = urlopen(request) Traceback (most recent call last): File "gpd-helper.py", line 35, in
Post #1 of 4 (6406 views) Permalink Problem with http://www.gossamer-threads.com/lists/python/python/641061 urllib2 and authentification Using this script for connect to Zope I have this error ---script: import urllib2 protocolo='http://' servidor='10.28.1.239/' pagina='manage' fullurl=protocolo+servidor+pagina aut=urllib2.HTTPBasicAuthHandler() aut.add_password(realm=None, uri=servidor, https://bytes.com/topic/python/answers/588903-urllib2-httpbasicauthhandler user='myadmin', passwd='mypass') opener=urllib2.build_opener(aut, urllib2.HTTPHandler(debuglevel=1)) print opener.open(fullurl).read() ---Error: connect: (10.28.1.239, 80) send: 'GET /manage HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: 10.28.1.239\r\nConnection: close\r\nUser-agent: Python-urllib/2.4\r\n\r\n' reply: 'HTTP/1.1 401 Unauthorized\r\n' header: Server: http error Zope/(Zope 2.10.5-final, python 2.4.4, win32) ZServer/1.1 header: Date: Tue, 22 Apr 2008 14:14:45 GMT header: Bobo-Exception-Line: 713 header: Content-Length: 884 header: Bobo-Exception-Value: See the server error log for details header: Content-Type: text/html; charset=iso-8859-15 header: Bobo-Exception-Type: Unauthorized header: Connection: close header: Bobo-Exception-File: HTTPResponse.py header: WWW-Authenticate: basic realm="Zope" Traceback (most http error 401 recent call last): File "z.py", line 15, in ? print opener.open(fullurl).read() File "/usr/local/lib/python2.4/urllib2.py", line 364, in open response = meth(req, response) File "/usr/local/lib/python2.4/urllib2.py", line 471, in http_response response = self.parent.error( File "/usr/local/lib/python2.4/urllib2.py", line 402, in error return self._call_chain(*args) File "/usr/local/lib/python2.4/urllib2.py", line 337, in _call_chain result = func(*args) File "/usr/local/lib/python2.4/urllib2.py", line 480, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Unauthorized why not send authentification? I try python 2.5 on slackware 12 too on python 2.4 and 2.5 on windows xp All same error -- ________________________________________ Lo bueno de vivir un dia mas es saber que nos queda un dia menos de vida -- http://mail.python.org/mailman/listinfo/python-list rw at smsnet Apr22,2008,11:24AM Post #2 of 4 (6316 views) Permalink Re: Problem with urllib2 and authentification [In reply to] "Miguel Beltran R." <yourpadre [at] gmail> writes: > Using this script for connect to Zope I have
and get tips & solutions from a community of 418,626 IT Pros & Developers. It's quick & easy. urllib2 and HTTPBasicAuthHandler P: n/a m.banaouas Hi all, I started to use urllib2 library and HTTPBasicAuthHandler class in order to authenticate with a http server (Zope in this case). I don't know why but it doesn't work, while authenticating with direct headers manipulation works fine! WinXP Sp2 Python 2.4.4 Thanks in advance for your help/comments # TestAuthHandler.py import base64, httplib, urllib2 httplib.HTTPConnection.debuglevel = 1 # def TestAuth(method): data = {'prot':'http','user':'admin','pass':'xxxxxx','hos t':'localhost','port':'8080','path':'manage','real m':'Zope'} url = '%(prot)s://%(host)s:%(port)s/%(path)s' % data req = urllib2.Request(url) # if (method == 'headers'): base64string = base64.encodestring('%s:%s' % (data['user'], data['pass']))[:-1] req.add_header("Authorization", "Basic %s" % base64string) elif (method == 'handler'): auth_url = '%(host)s:%(port)s/%(path)s' % data auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password(data['realm'], auth_url, data['user'], data['pass']) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) # f = urllib2.urlopen(req) data = f.read() print data # TestAuth('headers') TestAuth('handler') ------------------------- output: -------------------------