Hi there,
I’ve been banging my head against this one for 2 days now and I just can’t find the right syntax. I’ve connected to CH APIs before but using PHP, this time I’m trying to write a Python script and I’m falling at the first hurdle - authentication. Here’s my very simple code:
url = f"https://api.company-information.service.gov.uk/company/{company_number}"
keyEncoded = base64.b64encode(api_key.encode('utf-8'))
response = requests.get(url, auth=(keyEncoded,''))
This is just the current version - I’ve tried without encoding the API key, I’ve tried with and without : at the end of the API key, I’ve tried using Python’s HTTPBasicAuth, I’ve tried adding a header with auth in it… Every time, I get a 401 error - which suggests the problem is not with the key itself (and indeed, I’ve tested it in my PHP code and the key is fine) but the way in which I’m using it.
The code above is based on another post I found here on the forum, but it was a couple of years old. I’m sure I’m one iteration away from trying the right combo of encoding, colon, and auth method but I just can’t find it. Please help!