Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The provided API key is invalid. #304

Closed
K2ouMais opened this issue Mar 15, 2022 · 9 comments · Fixed by #305
Closed

The provided API key is invalid. #304

K2ouMais opened this issue Mar 15, 2022 · 9 comments · Fixed by #305
Labels
bug Something isn't working

Comments

@K2ouMais
Copy link

Hello everyone,

after update this package to the newest version all my tests are failing with the following error:
The provided API key is invalid.

Am I missing something?
The MEILISEARCH_KEY is set to null on my config file.

Thank you in advance

@alallema
Copy link
Contributor

Hi @K2ouMais,
Thank you for raising this issue. There is a breaking change in the release of this package on the Key Class. Maybe it's linked. Can you tell me more about the test who are failling?

@JodiLWilliams93
Copy link

JodiLWilliams93 commented Mar 16, 2022

I have the same issue when trying to generate indexes. It seems to be coming from here:

src/Http/Client.php

// lines 49-52
$this->headers = array_filter([
      'Authorization' => sprintf('Bearer %s', $this->apiKey),
      'User-Agent' => MeiliSearch::qualifiedVersion(),
]);

With no callback, array_filter only removes the empty entries, but because the Authorization string will have a value ('Bearer') even if the apiKey is null, the Authorization header is still being added. I was able to replicate the error in Postman by adding in the Authorization header with a value of 'Bearer'. Without the authorization header present, calls work fine.

This may not be the best way to handle it, but it seemed to fix it for me in the short term.

$this->headers = array_filter([
    'User-Agent' => MeiliSearch::qualifiedVersion(),
]);
if ($this->apiKey) {
    $this->headers['Authorization'] = sprintf('Bearer %s', $this->apiKey);
} 

@alallema alallema added the bug Something isn't working label Mar 16, 2022
@alallema
Copy link
Contributor

Hi @JodiLWilliams93,
Thank you for your feedback! You're right I will fix that.

@warmwhisky
Copy link

warmwhisky commented Jul 21, 2024

Hi I am getting this error also, but am on the latest release using Laravel and Scout "meilisearch/meilisearch-php": "^1.9",

I have set my env values as fol]lows

MEILI_MASTER_KEY=MASTER_KEY
MEILISEARCH_KEY=SEARCH_KY

In my meilisearch.toml config I have the master key set as follows

master_key = "MASTER_KEY"

When running

curl \
  -X GET 'http://localhost:7700/keys?limit=3' \
  -H 'Authorization: Bearer MASTER_KEY'

I get the correct keys that I have set in the .env however I always get The provided API key is invalid

Is there something I have missed?

@curquiza
Copy link
Member

Hello @warmwhisky

You should provide the master key (or the other key you want to use) in MEILISEARCH_KEY

See: https://www.meilisearch.com/docs/guides/back_end/laravel_scout#configuring-the-laravel-scout-driver

Also for your information, we are a small team, so we usually can’t spend much time doing support this specific repo 😢

So, if you want immediate help, we would recommend going to our Discord instead. The community will be able to help you, no matter your question.

Thanks for using Meilisearch!

@warmwhisky
Copy link

warmwhisky commented Jul 22, 2024

@curquiza thank you for the pointer. That has got me one step further. However the docs said to never use the master key on the front end search. Anyway now I know.

I had an issue with the discord link. It says that my invite is invalid? Edit: I have cleared my cookies for Disord and logged back in. Now the invite works. Thank you.

@curquiza
Copy link
Member

@curquiza thank you for the pointer. That has got me one step further. However the docs said to never use the master key on the front end search. Anyway now I know.

yes, use your public key if you are doing front end

@curquiza
Copy link
Member

curquiza commented Jul 22, 2024

Discord link: https://discord.meilisearch.com/

@warmwhisky
Copy link

warmwhisky commented Aug 21, 2024

So I am in the discord server and I do not have permission to post. Is there any way I can get help with The Authorization header is missing. It must use the bearer authorization method error? I don't understand how I got this to work on a different app only two days ago, but the new app in production simply says this for the same functions.

Edit: I spent two days trying to figure this out. I have now removed the api key so the websites will run. I am migrating servers and just do not have the time to figure it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants