Simon Simon - 1 year ago 115
Python Question

Python urllib2 Basic Auth Problem

Update: based on Lee's comment I decided to condense my code to a really simple script and run it from the command line:

import urllib2
import sys

username = sys.argv[1]
password = sys.argv[2]
url = sys.argv[3]
print("calling %s with %s:%s\n" % (url, username, password))

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

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

Unfortunately it still won't generate the
header (per Wireshark) :(

I'm having a problem sending basic AUTH over urllib2. I took a look at this article, and followed the example. My code:

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

req = urllib2.Request("")
f = urllib2.urlopen(req)
data =

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

GET /v1/user HTTP/1.1
Connection: close
Accept-Encoding: gzip
User-Agent: Python-urllib/2.5

You can see the Authorization is not sent, vs. when I send a request via curl:
curl -u user:password

GET /v1/user HTTP/1.1
Authorization: Basic =SNIP=
User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3
Accept: */*

For some reason my code seems to not send the authentication - anyone see what I'm missing?



Answer Source

The problem could be that the Python libraries, per HTTP-Standard, first send an unauthenticated request, and then only if it's answered with a 401 retry, are the correct credentials sent. If the Foursquare servers don't do "totally standard authentication" then the libraries won't work.

Try using headers to do authentication:

import urllib2, base64

request = urllib2.Request("")
base64string = base64.b64encode('%s:%s' % (username, password))
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)

Had the same problem as you and found the solution from this thread: