Vertex AI Gemini Auth Failed (401 UNAUTHENTICATED)
Vertex AI returned 401 UNAUTHENTICATED — Application Default Credentials (ADC) couldn't authenticate. Common causes: no credentials configured, service account missing roles/aiplatform.user, expired token not refreshed, or workload identity not attached in GKE/Cloud Run.
Ensure ADC works: local → gcloud auth application-default login; server → GOOGLE_APPLICATION_CREDENTIALS pointing to service account JSON; GKE/Cloud Run → workload identity attached with roles/aiplatform.user. Test with google.auth.default(). Confirm the correct project is selected.
The Error Messages You're Seeing
HTTP/1.1 401 Unauthorized
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials.
Expected OAuth 2 access token, login cookie or other valid
authentication credential.",
"status": "UNAUTHENTICATED"
}
}
Variants (Python):
google.auth.exceptions.DefaultCredentialsError: Could not
automatically determine credentials. Please set
GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials.
google.auth.exceptions.RefreshError:
('invalid_grant: Token has been expired or revoked.', ...)
google.api_core.exceptions.PermissionDenied: 403 Permission
'aiplatform.endpoints.predict' denied on resource
'projects/PROJECT/locations/us-central1/publishers/google/
models/gemini-2.5-pro'.ADC Credential Sources (Priority Order)
| Priority | Source | Set by |
|---|---|---|
| 1 | GOOGLE_APPLICATION_CREDENTIALS env var | Path to service account JSON |
| 2 | gcloud user credentials | gcloud auth application-default login |
| 3 | GCE / GKE / Cloud Run / Cloud Functions metadata server | Attached identity |
| 4 | Cloud Shell | Auto-attached |
Required IAM Roles
| Role | Purpose |
|---|---|
| roles/aiplatform.user | Basic Vertex AI calls (predict, generate) |
| roles/aiplatform.serviceAgent | Advanced (batch, deployment) |
| roles/serviceusage.serviceUsageConsumer | Bill against your project |
| roles/storage.objectViewer | Read GCS URIs (if using multimodal) |
What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1Diagnose Current Credentials
2Set Up Each Environment
Service account JSON files in code repos, containers, or env vars are a top source of credential leaks. Cloud Run and GKE with workload identity give you scoped, rotating credentials without any file to steal. Use JSON keys only when nothing else works.
3Handle Token Refresh in Long-Running Processes
Preventing This Error Going Forward
- Run diagnose_adc at app startup. Fail fast on misconfigurations.
- Prefer workload identity in cloud, gcloud in local dev.
- Grant aiplatform.user narrowly. Don't over-provision.
- Rotate JSON keys quarterly. Automate rotation via Secret Manager.
- Set quota project explicitly. Avoids "which project pays" errors.
- Handle Unauthenticated retries with credential refresh.
- Log SA identity + project on startup. Confirms right identity.
Researcher · AI Error Hub
Frequently Asked Questions
Some support for API keys on Vertex now exists, but the canonical path is service accounts + ADC. For enterprise workloads, stick with ADC; it works better with IAM, audit logging, and VPC-SC. API keys reserved for AI Studio simplicity.
Common: role granted at wrong scope (folder vs project), quota project not set, or credentials cached from previous SA. Refresh: gcloud auth application-default revoke && gcloud auth application-default login.
If the SA calling Vertex is in a different project than the one being billed. Same-project use doesn't require it. When cross-project, grant roles/serviceusage.serviceUsageConsumer on the billing project to the calling SA.
Yes: gcloud auth application-default login --impersonate-service-account=SA_EMAIL. Requires roles/iam.serviceAccountTokenCreator on the target SA. Use for dev/debugging, not production.
Cloud Logging → filter for protoPayload.methodName=~"generateContent" and severity=ERROR. Cross-reference with IAM logs. For token issues, enable audit logs for IAM and Vertex AI.
Related Errors
Master GCP Auth for Vertex AI
Weekly deep dives on service accounts, workload identity, and secure AI deployment. 12,000+ developers subscribe.
Subscribe Free →