google keyjson
Google key.json: The Complete Practical Playbook for Service Account Authentication
If you're working with Google Cloud Platform, Google Ads API, YouTube Data API, or any Google service that requires server-to-server authentication, you have almost certainly encountered a key.json file. Whether you're deploying your first service account or hardening an existing integration, this playbook covers everything you need to know — from creation to security best practices — in one actionable reference.
Key Takeaways
- key.json is not a Google product — it is the file format used to store service account private keys and OAuth 2.0 client credentials from Google Cloud.
- Never commit key.json files to version control. Add them to
.gitignoreimmediately after download; there is no revoke-and-replace for a committed key. - Service account keys have no secret rotation UI. You must create a new key, update your application, then delete the old one.
- Use Workload Identity Federation instead of key.json whenever you're running on Google Cloud — it eliminates long-lived secrets entirely.
- A single key.json can have broad IAM permissions. Always apply least-privilege roles at the project or folder level before downloading.
What Is a Google key.json File?
A key.json file is a JSON-formatted credential bundle issued by Google Cloud Identity and Access Management (IAM). It contains the private key material, client identifier, and metadata an application needs to authenticate as a service account without interactive user login.
Typical key.json Structure
{
"type": "service_account",
"project_id": "my-project-12345",
"private_key_id": "a1b2c3d4e5f6...",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkq...",
"client_id": "112233445566778899",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
}
Each field serves a specific purpose:
- type — Identifies the credential as a service account key.
- project_id — The GCP project the service account belongs to.
- private_key_id — A unique identifier for this specific key (used for key rotation).
- private_key — The RSA or EC private key used for signing JWT assertions.
- client_id — The unique service account identifier visible in logs and audit trails.
- token_uri — The endpoint the OAuth 2.0 library contacts to exchange a signed JWT for an access token.
When Do You Need a key.json?
Service account key files are the default authentication method in these common scenarios:
- Running a backend service that calls Google APIs server-to-server (no end-user consent flow).
- Authenticating with the Google Ads API, YouTube Data API v3, or Google Sheets API from a Python, Node.js, or Go application.
- Connecting third-party tools (data pipelines, CI/CD systems, monitoring agents) to GCP resources.
- Developing locally against Google Cloud services where Workload Identity is unavailable.
| Method | Best For | Lifetime | Security Risk |
|---|---|---|---|
| key.json (Service Account Key) | On-prem servers, third-party tools, local dev | Indefinite until manually rotated | High — long-lived secret |
| Workload Identity Federation | Google Cloud runtimes, GitHub Actions, AWS ECS | No stored secret; short-lived tokens | Low — no key material on disk |
| GCloud Application Default Credentials (ADC) | Local development, GKE, Cloud Run | Token refreshed automatically | Low — no user-managed secret |
| OAuth 2.0 consent flow | User-facing web/mobile apps | Refresh token + access token lifecycle | Medium — user delegation required |
| Identity-Aware Proxy (IAP) | Internal tool access over the internet | Session-based | Low — integrated with org SSO |
How to Create a Service Account Key JSON File
Follow this step-by-step procedure to create a service account and download its key.json securely.
Step 1 — Create the Service Account
- Navigate to GCP Console → IAM & Admin → Service Accounts.
- Click + Create Service Account.
- Assign a descriptive name (e.g.,
ads-api-reader) and add a clear description.
Step 2 — Assign the Minimum Required Role
- Search for only the roles your integration actually needs.
- Avoid Project Owner and Project Editor at all costs.
- Common granular roles:
roles/reader,roles/ads.admin,roles/youtube.actor. - Consider scoping to a specific resource (dataset, bucket) rather than the entire project.
Step 3 — Create and Download the Key
- Select the service account → Keys tab → Add Key → Create new key.
- Choose JSON as the key type.
- Click Create — the file downloads immediately. There is no second chance to download it.
Step 4 — Store the File Securely
- Move the file to a secrets manager (Secret Manager, AWS Secrets Manager, HashiCorp Vault).
- Set file permissions to
600(chmod 600 key.json). - Absolutely never commit to git, Docker images, or public repos.