# 401 Unauthorized - C#

**URL:** <http://forum.companieshouse.gov.uk/t/401-unauthorized-c/4067>\
**Category:** API Issues\
**Created:** [July 1, 2021, 10:07am UTC](http://forum.companieshouse.gov.uk/t/401-unauthorized-c/4067 "2021-07-01T10:07:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeyaprakash.ayyanan1](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/j/c67d28/32.png) [@jeyaprakash.ayyanan1](http://forum.companieshouse.gov.uk/u/jeyaprakash.ayyanan1)\
**Post date:** [July 1, 2021, 10:07am UTC](http://forum.companieshouse.gov.uk/t/401-unauthorized-c/4067/1 "2021-07-01T10:07:29Z")

</div>

Hello,

Can some one please help me with resolving the issue which I’m struggling to crack for 2 days? I keep on getting the unauthorized error using this code. I tried different things but ended with frustration.

```
            string authKey = "API_KEY";
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(authKey);
            var authKeyBase64String = System.Convert.ToBase64String(plainTextBytes);

            var url = new Uri("https://api.company-information.service.gov.uk/search/companies");
            var parameters = "?q=microsoft";
            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = url;
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authKeyBase64String);
                HttpResponseMessage httpResponse = await httpClient.GetAsync(parameters);
                if (httpResponse.IsSuccessStatusCode)
                {
                    var content = httpResponse.Content.ReadAsStringAsync().Result;
                    if (content != null)
                    {
                        Console.WriteLine(content);
                    }
                }
            }

```

This would be a great help for me.

---

<div class="post-metadata">

**Author:** ![MArkWilliams](http://forum.companieshouse.gov.uk/user_avatar/forum.companieshouse.gov.uk/markwilliams/32/194_2.png) [@MArkWilliams](http://forum.companieshouse.gov.uk/u/MArkWilliams)\
**Post date:** [July 1, 2021, 10:43am UTC](http://forum.companieshouse.gov.uk/t/401-unauthorized-c/4067/2 "2021-07-01T10:43:30Z")

</div>

Please use the search facility on this site for ‘unauthorized’ and read the posts and answers. It explains everything.  
I think your first problem though is the missing ‘:’ at the end of your API\_KEY …

---

<div class="post-metadata">

**Author:** ![jeyaprakash.ayyanan1](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/j/c67d28/32.png) [@jeyaprakash.ayyanan1](http://forum.companieshouse.gov.uk/u/jeyaprakash.ayyanan1)\
**Post date:** [July 1, 2021, 2:38pm UTC](http://forum.companieshouse.gov.uk/t/401-unauthorized-c/4067/3 "2021-07-01T14:38:55Z")

</div>

Thanks MarkWilliams.

I found my issue as I was using the TEST environment API key instead of PROD. I created a new key and its working now.
