

import urllib2
import sys

username = sys.argv[1]
password = sys.argv[2]
url = sys.argv[3]

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)

req = urllib2.Request(url)
f = urllib2.urlopen(req)
data = f.read()



passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "api.foursquare.com", username, password)

f = urllib2.urlopen(req)
data = f.read()

I'm seeing the following on the Wire via wireshark:

GET /v1/user HTTP/1.1
Host: api.foursquare.com
Connection: close
Accept-Encoding: gzip
User-Agent: Python-urllib/2.5 


GET /v1/user HTTP/1.1
Authorization: Basic =SNIP=
Host: api.foursquare.com
Accept: */*




  • 1


Try using headers to do authentication:

import urllib2, base64

result = urllib2.urlopen(request)


  • HTTP Error 505: HTTP Version Not Supported ;( May 29 '12 at 12:45
    – DerShodan
    May 3 '14 at 16:00
  • 3
    Note that you can simple call base64.b64encode instead of base64.encodestring and then you don't need to replace the newline.
    – Trey Stout
    Dec 2 '15 at 20:25
  • Thanks @TreyStout, I edited the solution to include your suggestion.
    – yayitswei
    Aug 19 '16 at 22:10
  • Similar problem here..In browser content of authorized page loaded and if i click cancel button i can see password page content's
    – Mostafa
    Aug 17 '18 at 18:37

(copy-paste/adapted from https://stackoverflow.com/a/24048772/1733117).


import urllib2
import base64

class PreemptiveBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
    '''Preemptive basic auth.

    def http_request(self, req):
        url = req.get_full_url()
        realm = None
        # this is very similar to the code from retry_http_basic_auth()
        # but returns a request object.
        user, pw = self.passwd.find_user_password(realm, url)
        if pw:
            req.add_unredirected_header(self.auth_header, auth)
        return req

    https_request = http_request

Then if you are lazy like me, install the handler globally

api_url = "http://api.foursquare.com/"
api_username = "johndoe"
api_password = "some-cryptic-value"

auth_handler = PreemptiveBasicAuthHandler()
    realm=None, # default realm.
opener = urllib2.build_opener(auth_handler)

Here's what I'm using to deal with a similar problem I encountered while trying to access MailChimp's API. This does the same thing, just formatted nicer.

import urllib2
import base64

chimpConfig = {
    "headers" : {
    "Content-Type": "application/json",
    "Authorization": "Basic " + base64.encodestring("hayden:MYSECRETAPIKEY").replace('\n', '')
    "url": 'https://us12.api.mailchimp.com/3.0/'}

#perform authentication
datas = None
request = urllib2.Request(chimpConfig["url"], datas, chimpConfig["headers"])
result = urllib2.urlopen(request)


passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  • 1
    – Simon
    Mar 9 '10 at 15:02
    – Lee
    Mar 9 '10 at 16:27
  • This will only work if the http response contains code 401 Unauthorized and the header 'WWW-Authenticate'; see this SO post answer. Mar 14 '12 at 8:38


  • Wil it allow to open urls like urllib2.urlopen('http://USER:PASS@example.com/path/')
    – ddofborg
    May 20 '16 at 17:13
  • This is another problem. Are you certain this doesn't work with the standard urllib2?
    – mcepl
    May 20 '16 at 22:43

Your Answer

