TBA API v3 401 Error C#

I’m trying to use the blue alliance to grab data, so there is less stress on our Scouters, but when I try reading the TBA status API I get a 401 error, I’m really stuck and need some help. I just don’t know what I’m doing wrong.

Code: private async void GetStatus()
{
var authData = “My-AuthKey”;
var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));

            var baseAddr = new Uri("https://www.thebluealliance.com/api/v3/");
            var client = new HttpClient { BaseAddress = baseAddr };
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-TBA-Auth-Key", authHeaderValue);
            
            var status = Convert.ToBase64String(Encoding.UTF8.GetBytes(await client.GetStringAsync("status"))); <- this is where the 401 error is thrown

           var returned = JsonConvert.DeserializeObject<List<TBA_Status>>(status);

            
        }

The X-TBA-Auth-Key shouldn’t be base64 encoded. It should just be the raw string.

1 Like

Thank you, I will try that.

I still get the same error, I went ahead and put the key in raw instead of converting it
this is what I have now:
var baseAddr = new Uri(“https://www.thebluealliance.com/api/v3/”);
var client = new HttpClient { BaseAddress = baseAddr };
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“X-TBA-Auth-Key”, “My-Auth-key”);

                var status = await client.GetStringAsync("status");

               var returned = JsonConvert.DeserializeObject<List<TBA_Status>>(status);

You shouldn’t be using DefaultRequestHeaders.Authorization.

Try headers.Insert("X-TBA-Auth-Key", "My-Auth-key").

https://docs.microsoft.com/en-us/uwp/api/windows.web.http.headers.httprequestheadercollection.insert

2 Likes

Ok, so progress I’m no longer getting the 401 error, but… The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.’
and i cant find how to fix it.

and is this right?

var baseAddr = new Uri("https://www.thebluealliance.com/api/v3/");
            var client = new HttpClient { BaseAddress = baseAddr };
            var request = new HttpRequestMessage();
            request.Headers.Referrer = baseAddr;
            request.Headers.Add("X-TBA-Auth-Key", "My-Auth-Key");
  var status = await client.GetStringAsync("status");

Maybe set the content type to be application/json?

Thank you, I will try that as soon as I get home. I’ll update you probably 5 hours from now, because I am busy for most of today.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.