The problem is that you are using the google.generativeai library which is the SDK for Google AI Studio and it is designed to us a simple API key. If you're using ADC then you need to use the Vertex AI SDK, googe-genai. This library is designed to automatically and securely use your environment's built-in service account.
I was able to get the following code to work:
pip install google-genai dotenv
import os
from dotenv import load_dotenv
from google.genai import Client
load_dotenv()
project_id = os.environ.get("PROJECT_ID")
location = os.environ.get("LOCATION")
print(f"[info] project id = {project_id}, location = {location}")
client = Client(
vertexai=True, project=project_id, location=location
)
model_name = "gemini-2.5-flash"
response = client.models.generate_content(
model=model_name,
contents="Hello there.",
)
print(response.text)
client.close()