CreateMessageAPI, Unauthorized: Missing bearer or basic authentication in header

async Task testexample()
{
    var client = new RestSharp.RestClient("https://api.openai.com/v1/");
    var request = new RestSharp.RestRequest($"threads/thread_L8******************JJ/messages/", RestSharp.Method.Post);
    request.AddHeader("Authorization", "Bearer ************");
    request.AddHeader("OpenAI-Beta", "assistants=v2");
    request.AddHeader("Content-Type", "application/json");
    request.AddParameter("application/json", "{\"role\":\"user\",\"content\":\"Hello, this is a test message.\"}", RestSharp.ParameterType.RequestBody);

    RestSharp.RestResponse response = await client.ExecuteAsync(request);
    Console.WriteLine("Response Status: " + response.StatusCode);
    Console.WriteLine("Response Body: " + response.Content);
} 

API key is hard-coded but i still get unauthorized, Missing bearer or basic authentication in header.
is this an openapi bug?, postman works great meaning my apikey allowed to call the api.
Please help.

I’ve faced a similar issue a while back, and the only thing I recall was that it was something stupid.

If the curl request works, I’d work from there and keep it as simple as possible and work from there.

https://chatgpt.com/share/81b2f5b0-161d-45a0-85b7-20831a5bf536

1 Like

Assuming your API key is correct, maybe try adding your API key to the client rather than the request using the JWT authenticator instead?

var options = new RestClientOptions("https://api.openai.com/v1/") {
    Authenticator = new JwtAuthenticator(sk**********);
};
var client = new RestClient(options);

this actually works, really stupid tho

1 Like

this doesnt work! unfortunately

1 Like

I did the same thing

[image]
    var request = new RestSharp.RestRequest($"threads/thread_L8******************JJ/messages/", RestSharp.Method.Post);
↓
    var request = new RestSharp.RestRequest($"threads/thread_L8******************JJ/messages", RestSharp.Method.Post);

This is caused by the trailing “/”