1

maybe someone can help me with this. I'm trying to get a data from a website using Python 2.7, using HTTP Basic Authentication. The only instructions the website provides is this:

In order for your web service client to authenticate using basic authentication, the first HTTPS request must include the "Authorization" header specifying the account credentials to authenticate with. The header for a such a request would look like this:

  GET /polcs/trade.xml HTTP/1.0
  Host: secure.pol.com
  Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

I tried few things but neither of them works, for example:

import urllib2
import base64

b = base64.encodestring('USERNAME:PASSWORD').replace('\n', '')
conf = {'headers': {'GET': '/polcs/trade.xml HTTP/1.0', 'Authorization': 'Basic ' + b},
    'url': 'secure.pol.com'}
datas = None
request = urllib2.Request(conf['url'], datas, conf['headers'])
result = urllib2.urlopen(request)

This one gives me HTTP Error 403:

url = 'https://secure.pol.com/polcs/data.xml'
request = urllib2.Request(url)
b = base64.encodestring('USERNAME:PASSWORD').replace('\n', '')
request.add_header("Authorization", "Basic " + b)
result = urllib2.urlopen(request)

Can someone please tell me where do I make a mistake trying to get that data? Thank you!

hod
  • 750
  • 7
  • 21
  • Can you try `base64.b64encode('USERNAME:PASSWORD')` instead of `base64.encodestring('USERNAME:PASSWORD')` to match the accepted answer here https://stackoverflow.com/a/2955687/4636715 – vahdet Jul 09 '20 at 12:59
  • @vahdet Same result. 403 Error – hod Jul 09 '20 at 13:17

0 Answers0