gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

API Error: API keys are not supported by this API when .env file exists

Open rsamborski opened this issue 1 year ago • 8 comments

What happened?

oAuth2 error appears when running Gemini CLI with API Key auth in a folder which contains .env for a Google Cloud project:

[API Error: API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See 
  https://cloud.google.com/docs/authentication (Status: UNAUTHENTICATED)]

The .env file contains:

GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_CLOUD_PROJECT=[PROJECT_ID]
GOOGLE_CLOUD_LOCATION=us-central1

A workaround is to switch to Google Login instead of an API_KEY auth.

What did you expect to happen?

API_KEY auth should work without errors.

Client information

$ gemini /about
CLI Version       0.1.9
Model                gemini-2.5-pro
Sandbox            no sandbox
OS                       linux
Auth Method    gemini-api-key
GCP Project       [PROJECT_ID] 

Login information

API key

Anything else we need to know?

No response

rsamborski avatar Jul 02 '25 12:07 rsamborski

cc @scidomino We debugged a similar issue last week. This is one step further, where the .env variables are intentionally present, but should NOT be used for the CLI.

fhinkel avatar Jul 03 '25 00:07 fhinkel

Do you have GEMINI_API_KEY or GOOGLE_API_KEY set in your environment variables? We recently changed the logic around this.

scidomino avatar Jul 03 '25 00:07 scidomino

I ran into this error with these in my local .env file

export GOOGLE_GENAI_USE_VERTEXAI=TRUE
export GOOGLE_CLOUD_PROJECT="......."
export GOOGLE_CLOUD_LOCATION="global"

but I know I also had some other crufty env variables as well.

zeroasterisk avatar Jul 03 '25 02:07 zeroasterisk

We've seen this error happen when you have either a GEMINI_API_KEY or GOOGLE_API_KEY and also GOOGLE_GENAI_USE_VERTEXAI=TRUE. Can someone confirm that they are seeing this issue and it is not the case that GEMINI_API_KEY or GOOGLE_API_KEY are set? It's not enough to show me the .env file because there are other ways to set environment variables. run printenv (or your os's equivalent) to see what's in your environment variables.

scidomino avatar Jul 03 '25 03:07 scidomino

same issue. I have configured the API Key in the ~/.gemini/.env and it works fine, but if there is .env in the directory when gemini starts, the API Key will be required. It seems that gemini cli requires the API Key as long as it detects that there is a .env file in the current directory, instead of using the API Key in ~/.gemini/.env first.

mingbai2006 avatar Jul 03 '25 08:07 mingbai2006

Preferring the .env file in the working directory over the one in the home directory is intentional. The working dir configs are considered more specific than the ones in the home dir.

scidomino avatar Jul 03 '25 13:07 scidomino

Preferring the .env file in the working directory over the one in the home directory is intentional. The working dir configs are considered more specific than the ones in the home dir.

Thanks for reply, but I think the logic should be based on variables, not files. If the env in the working directory does not have an API KEY, the one in the home directory should be used instead of requiring login. This is a problem because env files exist in many projects, such as vite project. Keeping this behavior will cause some troubles

mingbai2006 avatar Jul 04 '25 02:07 mingbai2006

Preferring the .env file in the working directory over the one in the home directory is intentional. The working dir configs are considered more specific than the ones in the home dir.

Thanks for reply, but I think the logic should be based on variables, not files. If the env in the working directory does not have an API KEY, the one in the home directory should be used instead of requiring login. This is a problem because env files exist in many projects, such as vite project. Keeping this behavior will cause some troubles

Merging .env files would leave users with no way to unset a variable. That's not scalable. We intentionally load only the single most specific .env file for each execution. If you intentionally create an .env file in your working directory, you are going to have to remember to add the API key to it as well. There's no way around that.

scidomino avatar Jul 04 '25 04:07 scidomino