Python Httperror Http Error 401 Unauthorized
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might python 3 urllib.error.httperror http error 401 unauthorized have Meta Discuss the workings and policies of this site About python3 urllib.error.httperror: http error 401: unauthorized Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads httppasswordmgrwithdefaultrealm 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
Typeerror: 'bytes' Object Is Not Callable
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 into this problem. I do have the username and password, but I don't know how to python urllib2 authentication example 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 add a comment| up vote 4 down vote I think you can use requests module which would make it be more easy for you. import requests username = 'user' password = 'pass' ur
here for a quick overview of the site Help Center Detailed answers to any questions you might httpbasicauthhandler have Meta Discuss the workings and policies of this site
Python Sharepoint
About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting
Python Requests Download File
ads 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 http://stackoverflow.com/questions/10195915/python-urllib2-httperror-http-error-401-unauthorized million programmers, just like 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 http://stackoverflow.com/questions/26451173/http-error-401-unauthorized-when-querying-youtube-api-for-playlist-with-pytho always get a 401 Error 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
here for a quick overview of the http://stackoverflow.com/questions/21729255/urllib2-httperror-http-error-401-unauthorized site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn https://bytes.com/topic/python/answers/588903-urllib2-httpbasicauthhandler more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation http error Tags Users Badges Ask Question x 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 urllib2.HTTPError: HTTP Error 401: Unauthorized up vote 5 down vote http error 401 favorite My requirement is to download an abc.zip file from some website http://clientdownload.xyz.com/Documents/abc.zip For this activity I have written a python script as follows: url_to_check = 'http://clientdownload.xyz.com/Documents/abc.zip' username = "user" password = "pwd" p = urllib2.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, url_to_check, username, password) handler = urllib2.HTTPBasicAuthHandler(p) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) zip_file = urllib2.urlopen(url_to_check).read() file_name = 'somefile.zip' meta = zip_file.info() file_size = int(meta.getheaders("Content-Length")[0]) print "Downloading: %s Bytes: %s" % (file_name, file_size) with open(file_name, 'wb') as dwn_file: dwn_file.write(zip_file.read()) Whereas I am getting the following errors when I run the script: File "updateCheck.py", line 68, in check_update zip_file = urllib2.urlopen(url_to_check).read() File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 406, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 519, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 444, in error return self._call_chain(*
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: -------------------------