Alines Webマガジン

google credentialsjson

📅 October 29, 2026📖 Verified Publication
```html Google credentials.json: Complete Guide to Setup, Usage & Security (2025)

Google credentials.json: Complete Guide to Setup, Usage & Security (2025)


What Is a Google credentials.json File?

A credentials.json file is a JSON-formatted configuration file containing OAuth 2.0 client identifiers issued when you register an application in the Google Cloud Console. It typically includes the following fields:

Core fields in a Google credentials.json OAuth file
Field Purpose Example Value
installed.client_id Unique identifier for your OAuth client 123456789-abc.apps.googleusercontent.com
installed.client_secret Secret key used to prove your app's identity during token exchange GOCSPX-xxxxxxxxxxxxxxxx
installed.auth_uri Google's OAuth 2.0 authorization endpoint https://accounts.google.com/o/oauth2/auth
installed.token_uri Endpoint used to exchange authorization codes for access tokens https://oauth2.googleapis.com/token
installed.redirect_uris Authorized callback URLs that Google will accept ["http://localhost:8080/oauth2callback"]
installed.client_name Human-readable name of your application

This file is primarily used in server-side or desktop applications where users must grant your app permission to access their Google services (Drive, Gmail, Calendar, etc.) through the OAuth consent flow.

credentials.json vs. Service Account Key File

One of the most frequent sources of confusion is the difference between an OAuth credentials.json and a Service Account key file. Both are JSON files downloaded from the Google Cloud Console, but they serve fundamentally different purposes.

OAuth credentials.json vs. Service Account key file
Attribute OAuth credentials.json Service Account Key (.json)
Issued for User-facing OAuth consent flow Server-to-server authenticated requests
Auth mechanism Authorization code + refresh token RSA/ECDSA signed JWT
User involvement Required (user clicks "Allow") None (automated)
Scope of access Limited to granted user scopes All scopes enabled on the service account
Typical use case Apps accessing a user's Drive or Gmail Background jobs, CI/CD, server APIs
Security risk if leaked High — client_secret enables token theft Critical — full service account impersonation
File name convention credentials.json Usually service-account-key.json

How to Generate a credentials.json File

Step 1 — Create a Google Cloud Project

  1. Navigate to console.cloud.google.com.
  2. Select or create a new project.
  3. Ensure billing is enabled if the APIs you need require it.

Step 2 — Enable the Required APIs

Go to APIs & Services > Library and enable every API your application will call (e.g., Google Drive API, Gmail API, Sheets API).

Step 3 — Create OAuth 2.0 Credentials

  1. Go to APIs & Services > Credentials.
  2. Click Create Credentials > OAuth client ID.
  3. Choose Application type: Desktop app (for local/server apps) or Web application (for server-side callbacks).
  4. Set the Name and configure Authorized redirect URIs carefully — these must exactly match what your code expects.
  5. Click Create and download the JSON file.

Step 4 — Save and Name the File

Save the downloaded file as credentials.json in your project root directory. Most Google client libraries look for this exact filename by default.

Pro tip: If you are using Python's google-auth or google-auth-oauthlib libraries, the default behavior automatically loads credentials.json from the current working directory. Renaming your file breaks the flow silently.

How to Use credentials.json in Your Application

Python Example (OAuth Consent Flow)

from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import Flow
import pickle
import os

# Load the credentials file
flow = Flow.from_client_secrets_file(
    'credentials.json',
    scopes=['https://www.googleapis.com/auth/drive.readonly']
)

# Direct the user to the OAuth consent screen
auth_url, _ = flow.authorization_url(prompt='consent')
print(f'Visit this URL: {auth_url}')

# After user grants access, exchange the code for tokens
code = input('Enter the authorization code: ')
flow.fetch_token(code)

# Save the credentials for future use
with open('token.pickle', 'wb') as token:
    pickle.dump(flow.credentials, token)

Node.js Example

const {OAuth2Client} = require('google-auth-library');
const fs = require('fs');

const client = new OAuth2Client(
  'credentials.json'  // Library reads client_id, client_secret, redirect_uris
);

const authUrl = client.generateAuthUrl({
  access_type: 'offline',
  scope: ['https://www.googleapis.com/auth