"message": "You didn't provide an API key. You need to provide your API key in an Authorization header.. (solved)

Greetings.
My application is already in production and has stopped working displaying the message:“message”: "You didn’t provide an API key. You need to provide your API key in an Authorization header using Bearer

can you help me?
Thank you very much.

Here’s the code:

                function consome_gpt() {

                    // Chave da API do OPENAI
                    const OPENAI_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; (Fictitious key)

                    // Requisição para chatgpt
                    fetch("https://api.openai.com/v1/completions", {

                        // Método para enviar os dados
                        method: "POST",

                        // Dados ennviados no cabeçalho da requisição
                        headers: {
                            Accept: "application/json",
                            "Content-Type": "application/json",
                            Authorization: "Bearer " + OPENAI_API_KEY,
                        },

                        // Enviar os dados no corpo da requisição
                        body: JSON.stringify({
                            model: "text-davinci-003", //Modelo
                            prompt: pergunta, // Texto da pergunta
                            max_tokens: 2048, // Tamanho da resposta
                            temperature: 0.1 // Criatividade na resposta
                        }),
                    })
                        // Acessa o then quando obtiver resposta
                        .then((resposta) => resposta.json())
                        .then((dados) => {
                            //console.log(dados);
                            //   console.log(dados.choices[0].text);

                            // Enviar o texto da resposta para a página HTML
                            document.getElementById('resposta').innerHTML = dados.choices[0].text;
                            localStorage.setItem("dados", dados.choices[0].text);
                            var dadostexto = localStorage.getItem("dados");
                            app.dialog.close();

                        })
                        // Retorna catch quando gerar erro
                        .catch(() => {
                            // Enviar o texto da resposta para a página HTML
                            app.dialog.close();
                            document.getElementById('resposta').innerHTML = "Sem resposta";

                        });
                };

Do you have credits in your account?

Did you check your keys on the OpenAI page? Everything good?

If your API key appears in public, OpenAI will automatically negate it.

1 Like

Hello, thanks for the response.

I use the API in an App that has been published on Google for 30 days.
It stopped working today.
I tested it in development and it doesn’t work either.

Do you have credits in your account?
Yes

Did you check your keys on the OpenAI page? Everything good?
Yes

If your API key appears in public, OpenAI will automatically negate it.
Where can I check this information?

Thanks

Did you have the API key embedded in the Google app? If so, it likely got leaked and OpenAI turned it off when they noticed maybe?

Are you sure you’re providing an API key? It’s not saying it’s wrong, it’s saying it’s missing.

Did you have the API key embedded in the Google app? If so, it likely got leaked and OpenAI turned it off when they noticed maybe?

Yes, the key is embedded in the App.
I’ll change it to see if it solves it.

Are you sure you’re providing an API key? It’s not saying it’s wrong, it’s saying it’s missing.

The key was reported correctly as it worked for 30 days

After adjusting I will let you know if it worked.

Thanks for the tips.

You really don’t want to do this…

Your app should call back to your server which then calls the OpenAI API, gets the response, and forwards it to the user.

There is so much that can go wrong when you hand your keys over to the user, a big one being you can’t easily do the kind of message filtering you need to do to ensure your whole account doesn’t get nuked for policy violations.

Another is that you can’t track usage by user. Say you have 100 users and your API bill is $99.99, you might be thinking, great! My users use about $1 worth of API calls each, so I’ll bill accordingly… Well, if one user is using $99 worth of API and the other 99 are using $0.01, when you start charging people $2/month you are going to end up with only one user and empty pockets.

1 Like

If the app sends the key to openAI, then there is a very good chance that bad actors intercept it using a proxy and use it. Your key must be stored only on a backend and the app should communicate with your backend and not openAI.

1 Like

Friends.
Problem solved.
The error was in the model, it used text-davinci-003 which was replaced by gpt-3.5-turbo-instruct
on 01/04/2024.

Thanks!

My Key is private and am getting 401 error. Please guide!

401 - Invalid Authentication Cause: Invalid Authentication
Solution: Ensure the correct API key and requesting organization are being used.
401 - Incorrect API key provided Cause: The requesting API key is not correct.
Solution: Ensure the API key used is correct, clear your browser cache, or generate a new one.
401 - You must be a member of an organization to use the API Cause: Your account is not part of an organization.
Solution: Contact us to get added to a new organization or ask your organization manager to invite you to an organization.

https://platform.openai.com/docs/guides/error-codes

Also how do we know if it is public? I used setx on windows and system variables

I do have an organization. Who do I contact to get added to a new organization ?

It’s here in the error code. :wink: