Gemini API Key restricted from using googleSearch tool with gemini-3.1-flash-lite

My app uses the googleSearch tool with gemini-3.1-flash-lite. It works fine in Google AI Studio but gives an error when deployed to Cloud Run.

I created an API key via “APIs & Services/Credentials/Create Credentials/API key” which is authorized to access “Gemini API” and is bound to a Service Account. My project is tied to a Billing Account.

Using that key (or the key provided by Google AI Studio), I get this error:

ApiError: {“error”:{“code”:400,“message”:“API Key not found. Please pass a valid API key.”,“status”:“INVALID_ARGUMENT”,“details”:[{“@type”:“type.googleapis.com/google.rpc.ErrorInfo",“reason”:“API_KEY_INVALID”,“domain”:“googleapis.com”,“metadata”:{“service”:“generativelanguage.googleapis.com”}},{“@type”:“type.googleapis.com/google.rpc.LocalizedMessage”,“locale”:“en-US”,“message”:"API Key not found. Please pass a valid API key.”}]}}

Looks like the API Key is restricted from using the googleSearch tool.