I’m experiencing a 401 Unauthorized error when using my Gemini API key generated from AI Studio. My account is only generating keys in the new AQ. format, but these keys are failing with 401 errors when making direct REST calls to the Gemini API endpoint.
Endpoint I’m calling:
https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={API_KEY}
Error received:
HTTP Error 401: Unauthorized
What I have confirmed:
-
The key is being passed correctly — I have verified the exact key value reaching my server via startup logs
-
The key is not expired or rotated — I just generated it fresh
-
The model name
gemini-2.0-flashis correct -
The request format matches the official documentation exactly
-
The issue is reproducible across multiple fresh keys generated from the same account
Root cause identified: My account appears to only generate keys in the new AQ. token format. These tokens are OAuth-style access tokens and appear to be incompatible with direct REST API key authentication (?key= query parameter). The standard AIza... format keys that work with direct REST calls are no longer being generated for my account.
What I am requesting: Please restore my account’s ability to generate standard AIza... format API keys, or advise on the correct authentication method for direct urllib REST calls without using the google-generativeai Python SDK.
Why I cannot use the SDK: My deployment environment (Railway) has restricted package installations and I am making raw urllib calls. I cannot use the google-generativeai package.
Thank you.