# Invalid Authorization Header

**URL:** <http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328>\
**Category:** API Issues\
**Created:** [January 15, 2024, 11:20am UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328 "2024-01-15T11:20:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![azamat.ilyassov](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@azamat.ilyassov](http://forum.companieshouse.gov.uk/u/azamat.ilyassov)\
**Post date:** [January 15, 2024, 11:20am UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/1 "2024-01-15T11:20:12Z")

</div>

Hello. I am not good with using APIs and HTTP requests.  
Company asked to scraped data from Companies House.  
I am making some test requests in Python, but I get

```auto
{"error":"Invalid Authorization header","type":"ch:service"}

```

My code is:

```python
payload={}
headers = {'Authorization': 'API my_api_key'}
url_cust = "https://api.company-information.service.gov.uk/company/01234567/"
rep = requests.request("GET", url_cust, headers=headers, data=payload, verify=False)

```

Can anyone help me out with this? I think the problem is either with my request, or with the way I generated my API key(no restricted IPs specified, no JavaScript domains)

---

<div class="post-metadata">

**Author:** ![ebrian101](http://forum.companieshouse.gov.uk/user_avatar/forum.companieshouse.gov.uk/ebrian101/32/1145_2.png) [@ebrian101](http://forum.companieshouse.gov.uk/u/ebrian101)\
**Post date:** [January 15, 2024, 12:38pm UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/2 "2024-01-15T12:38:30Z")

</div>

The problem is with your authorisation header. Try this:

```auto
import base64
encoded_credentials = base64.b64encode('api_key:'.encode("utf-8")).decode("utf-8")
headers = {'Authorization': f"Basic {encoded_credentials}"}

```

The header should be the word `Basic` followed by a space, followed by the base64 encoding of `api_key:` (note the colon after your api key!).

---

<div class="post-metadata">

**Author:** ![azamat.ilyassov](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@azamat.ilyassov](http://forum.companieshouse.gov.uk/u/azamat.ilyassov)\
**Post date:** [January 15, 2024, 2:29pm UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/3 "2024-01-15T14:29:20Z")

</div>

Thank you very much for your reply. I tried this, but now I have this error:

```auto
{"error":"Invalid Authorization","type":"ch:service"}

```

Basically the same, but now just Invalid Authorization. Now I may assume this is the problem of me generating API keys incorrectly.

---

<div class="post-metadata">

**Author:** ![azamat.ilyassov](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@azamat.ilyassov](http://forum.companieshouse.gov.uk/u/azamat.ilyassov)\
**Post date:** [January 16, 2024, 9:59am UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/4 "2024-01-16T09:59:50Z")

</div>

Update:

I found a solution to the “Invalid Authorization” problem on the following post:  
[Postman API call - API Issues - Companies House Developer Forum (chdev.org)](https://forum.aws.chdev.org/t/postman-api-call/3740)

I changed Application environment from Test to Live.

---

<div class="post-metadata">

**Author:** ![aucourantcs](http://forum.companieshouse.gov.uk/user_avatar/forum.companieshouse.gov.uk/aucourantcs/32/1645_2.png) [@aucourantcs](http://forum.companieshouse.gov.uk/u/aucourantcs)\
**Post date:** [January 17, 2024, 10:00am UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/5 "2024-01-17T10:00:08Z")

</div>

If you are new to CompaniesHouseAPI it’s always best to set up your POSTMAN first, make a few requests and understand how the Authorization headers work. Once that is taken care of - putting that in code becomes a lot easier. Do note you will need OAuth 2 for certain use cases → [Authentication](https://developer.company-information.service.gov.uk/authentication)

---

<div class="post-metadata">

**Author:** ![gajdonid](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/g/41988e/32.png) [@gajdonid](http://forum.companieshouse.gov.uk/u/gajdonid)\
**Post date:** [January 19, 2024, 12:15pm UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/6 "2024-01-19T12:15:52Z")

</div>

Dear @azamat.ilyassov ,

firstly you need to generate an API\_Key at your Developer Hub account([How to regenerate an API Key - #3 by MArkWilliams](https://forum.aws.chdev.org/t/how-to-regenerate-an-api-key/1126/3)).  
Then the request in Python is straightforward:  
r = requests.get(url, auth=(API\_Key , “”))

where API\_Key is the generated API\_Key by which the server authenticates you.

---

<div class="post-metadata">

**Author:** ![kyle](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](http://forum.companieshouse.gov.uk/u/kyle)\
**Post date:** [March 18, 2025, 9:01am UTC](http://forum.companieshouse.gov.uk/t/invalid-authorization-header/7328/7 "2025-03-18T09:01:39Z")

</div>

This was my problem too.
