httplib with NETRC authentication

P

pratibha natani

Hi,

I am trying to establish http connection to a gerrit host using netrc authentication. I have a netrc file created with following entries:
machine host1.com login name password pass

I did a debug and saw that my netrc file is being read correctly. Also in the connection object(after sending request) a header got created with appropriate credentials:
'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}

Still I get 401,Unauthorized in response. Any help would be greatly appreciated!

headers = headers or {}
bare_host = host.partition(':')[0]
auth = NETRC.authenticators(bare_host)
if auth:
headers.setdefault('Authorization', 'Basic %s' % (
base64.b64encode('%s:%s' % (auth[0], auth[2]))))
print "inside auth 0 ", auth[0]
print "inside auth 2 ", auth [2]
if body:
body = json.JSONEncoder().encode(body)
headers.setdefault('Content-Type', 'application/json')
conn = httplib.HTTPSConnection(host)
conn.req_host = host
conn.req_params = {
'url': '/a/%s' % path,
'method': reqtype,
'headers': headers,
'body': body,
}
conn.request(**conn.req_params)
print "conn ", conn.__dict__


Thanks
Pratibha
 
C

Chris Angelico

I am trying to establish http connection to a gerrit host using netrc authentication. I have a netrc file created with following entries:
machine host1.com login name password pass

I did a debug and saw that my netrc file is being read correctly. Also in the connection object(after sending request) a header got created with appropriate credentials:
'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}

Still I get 401,Unauthorized in response. Any help would be greatly appreciated!

The obvious question is: What *does* work? Does it work when you use
wget, or some other application? Then go and look at what that sends
for its authentication headers. Tip: It won't be "Authorization".

ChrisA
 
A

Akira Li

pratibha natani said:
Hi,

I am trying to establish http connection to a gerrit host using netrc authentication. I have a netrc file created with following entries:
machine host1.com login name password pass

I did a debug and saw that my netrc file is being read correctly. Also in the connection object(after sending request) a header got created with appropriate credentials:
'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}
It should be QWxhZGRpbjpvcGVuIHNlc2FtZQ== (note: the second '=')

Also, make sure that you use the correct case for the username. Userids
might be case sensitive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top