google credentialsjson
Google credentials.json: The Complete Guide to Service Account Authentication
Key Takeaways
- A
credentials.jsonfile is a service account key — a JSON-formatted credential that lets applications authenticate to Google APIs without interactive login. - Generate it from Google Cloud Console under IAM & Admin > Service Accounts by creating a new JSON key for your chosen service account.
- Never commit credentials.json to version control — add it to
.gitignoreand use environment variables or secret managers in production. - The file contains sensitive private keys; anyone with it can impersonate the service account and access every permission granted to it.
- Rotate keys regularly and delete unused ones to reduce the attack surface and comply with security best practices.
What Is a Google credentials.json File?
A credentials.json file is the exported service account key generated by Google Cloud Platform (GCP). It stores the cryptographic identity of a service account in JSON format, containing fields that allow an application to prove its identity to Google's authorization servers.
What Does the File Contain?
When you download a JSON key from the Google Cloud Console, the resulting file includes the following core fields:
| Field | Purpose |
|---|---|
type |
Always set to "service_account"; tells the library which auth mechanism to use. |
project_id |
The GCP project ID the service account belongs to. |
private_key_id |
Unique identifier for the private key; used to select the correct key during token signing. |
private_key |
The RSA private key used to sign JWTs for OAuth 2.0 token exchange. Keep this secret. |
client_email |
The email address of the service account (e.g., my-sa@project.iam.gserviceaccount.com). |
client_id |
A numeric identifier for the service account, used in certain API calls. |
auth_uri |
The OAuth authorization endpoint URL. |
token_uri |
The URL used to exchange a signed JWT for an OAuth access token. |
auth_provider_x509_cert_url |
URL where Google publishes the public certificates for this key. |
client_x509_cert_url |
URL for the service account's X.509 certificate. |
How to Generate a Google credentials.json File
Follow these steps to create a new service account key from the Google Cloud Console:
- Open the Google Cloud Console (console.cloud.google.com) and select your project.
- Navigate to IAM & Admin > Service Accounts.
- Click + Create Service Account, assign a name and description, then click Finish.
- Select the roles the service account needs (e.g.,
Storage Object Admin,BigQuery Data Editor). Assign only the minimum required permissions. - Click + Create Key, choose JSON as the key type, and click Create.
- The
credentials.jsonfile downloads automatically. Store it securely.
Generate via gcloud CLI (Alternative Method)
If you prefer the command line, run the following:
gcloud iam service-accounts create my-service-account \
--display-name="My Service Account"
gcloud iam service-accounts keys create credentials.json \
--iam-account=my-service-account@your-project.iam.gserviceaccount.com
How to Use credentials.json in Your Application
Once you have the credentials.json file, your application can load it to authenticate against Google APIs. Below are the most common implementation patterns.
Python (Google Auth Library)
from google.oauth2 import service_account
from googleapiclient.discovery import build
credentials = service_account.Credentials.from_service_account_file(
'credentials.json',
scopes=['https://www.googleapis.com/auth/spreadsheets.readonly']
)
service = build('sheets', 'v4', credentials=credentials)
Node.js (google-auth-library)
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth({
keyFile: 'credentials.json',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
const client = await auth.getClient();
const accessToken = await client.getAccessToken();
Java (Google Auth Library for Java)
GoogleCredentials credentials = GoogleCredentials
.fromStream(new FileInputStream("credentials.json"))
.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/bigquery"
));
Common Problems and How to Solve Them
Problem 1: "InvalidGrant" or "Unauthorized" Errors
If your requests return 401 Unauthorized or invalid_grant, check the following:
- Verify the
scopesyou're requesting match the API permissions the service account actually has. - Ensure the service account has been granted the correct roles in IAM > Members.
- Confirm the
credentials.jsonfile has not been rotated or deleted in the Cloud Console. - Check that the project ID inside the JSON matches the project where the APIs are enabled.
Problem 2: Permission Denied Even With Correct Credentials
Service account keys authenticate identity — they do not automatically grant access. If you receive 403 Permission denied:
- Assign the appropriate IAM role to the service account in the target project.
- For Google Workspace domains, enable domain-wide delegation and grant the service account the required OAuth scopes.
- Make sure the target API is enabled in APIs & Services > Library.
Problem 3: "File Not Found" When Loading credentials.json
Applications often fail to locate the JSON file because the relative path is incorrect. Always resolve the absolute path at runtime:
import os
from google.oauth2 import service_account
base_path = os.path.dirname(os.path.abspath(__file__))
key_path = os.path.join(base_path, 'credentials.json')
credentials = service_account.Credentials.from_service_account_file(
key_path, scopes=['https://www.googleapis.com/auth/bigquery']
)
Security Checklist for Google credentials.json
| Check | Priority | Status |
|---|