This guide covers the complete OAuth 2.0 authentication process for the Google Drive MCP Server, including initial setup, token management, and troubleshooting.
Before starting authentication, ensure you have completed:
- Initial Setup Guide - Google Cloud project and local installation
- OAuth credentials downloaded and placed in
credentials/gcp-oauth.keys.json - Encryption key generated and saved in
.envfile - Required APIs enabled in Google Cloud Console
The server uses a sophisticated authentication system with multiple components:
Authentication Flow:
User → OAuth Browser Flow → Google Authorization → Tokens → Encrypted Storage
↓ ↓
Runtime → Token Validation → Auto-Refresh → API Access → Continuous Operation
- AuthManager: Handles OAuth flow and token lifecycle
- TokenManager: Encrypts/decrypts and stores tokens securely
- Auto-Refresh: Proactive token refresh (30-minute intervals)
- Health Monitoring: Continuous token status monitoring
- Audit Logging: Complete authentication event logging
# Verify all prerequisites are met
cd /path/to/gdrive
# Check OAuth keys exist
ls -la credentials/gcp-oauth.keys.json
# Should show file with 600 permissions
# Check encryption key is set
source .env
echo $GDRIVE_TOKEN_ENCRYPTION_KEY | base64 -d | wc -c
# Should output: 32
# Verify build is complete
ls -la dist/index.js
# Should exist and be executable
# Test environment loading
node -e "require('dotenv').config(); console.log('Environment loaded:', !!process.env.GDRIVE_TOKEN_ENCRYPTION_KEY)"Method 1: Direct Node.js (Recommended for Local Development)
# Ensure environment variables are loaded
source .env
# Run authentication (this will open your browser)
node ./dist/index.js auth
# Expected output:
# "Starting OAuth flow..."
# "Opening browser for authentication..."
# "Waiting for authorization..."Method 2: Using Auth Script (Recommended for Docker Setup)
# Use the provided authentication script
./scripts/auth.sh
# This script will:
# - Check for required files
# - Generate encryption key if needed
# - Run authentication flow
# - Verify success-
Browser Opens Automatically
- Your default browser will open with Google OAuth page
- If browser doesn't open, check terminal for URL to copy
-
Choose Google Account
- Select the Google account you want to use
- Must be an account with access to your Google Drive
-
OAuth Consent Screen
Google Drive MCP Server wants to: ✓ See, edit, create, and delete all of your Google Drive files ✓ See, edit, create, and delete your spreadsheets in Google Drive ✓ See, edit, create, and delete your documents in Google Drive ✓ See, edit, create, and delete all your Google Forms forms ✓ View your Google Apps Script projects -
Grant Permissions
- Review the permissions carefully
- Click "Allow" to grant access
- Do not click "Deny" or close the browser
-
Success Confirmation
- Browser will show "Authentication successful!"
- You can close the browser tab
- Terminal will show "Authentication completed successfully"
# Check if token file was created
ls -la credentials/.gdrive-mcp-tokens.json
# Should exist with 600 permissions
# Run health check to verify tokens work
node ./dist/index.js health
# Expected output:
{
"status": "HEALTHY",
"timestamp": "2024-01-15T...",
"checks": {
"tokenStatus": {
"status": "pass",
"message": "Token is valid and not expiring soon"
},
"refreshCapability": {
"status": "pass",
"message": "Refresh token available"
}
}
}# Test server startup (Ctrl+C to stop)
node ./dist/index.js
# Expected output:
# "MCP Server for Google Drive initialized"
# "AuthManager initialized successfully"
# "Server running on stdio transport"
# In another terminal, test a simple operation
echo '{"method": "tools/list"}' | node ./dist/index.js
# Should return list of available toolsTokens are stored in encrypted format:
# View token file (encrypted content)
cat credentials/.gdrive-mcp-tokens.json
# Shows encrypted data, not readable tokens
# Check audit log for token events
tail -f logs/gdrive-mcp-audit.log
# Shows token lifecycle eventsThe server automatically refreshes tokens:
- Refresh Interval: Every 30 minutes (configurable)
- Preemptive Refresh: 10 minutes before expiry (configurable)
- Retry Logic: Up to 3 retries with exponential backoff
- Health Monitoring: Continuous token validity checks
# Monitor token refresh in real-time
tail -f logs/combined.log | grep -i token
# Configure refresh intervals in .env
echo "GDRIVE_TOKEN_REFRESH_INTERVAL=1800000" >> .env # 30 minutes
echo "GDRIVE_TOKEN_PREEMPTIVE_REFRESH=600000" >> .env # 10 minutes
echo "GDRIVE_TOKEN_MAX_RETRIES=3" >> .envThe system logs all token events:
// Example audit log entries
{"timestamp":"2024-01-15T10:00:00.000Z","event":"TOKEN_ACQUIRED","metadata":{"expires_in":3600}}
{"timestamp":"2024-01-15T10:25:00.000Z","event":"TOKEN_REFRESHED","metadata":{"new_expires_in":3600}}
{"timestamp":"2024-01-15T10:30:00.000Z","event":"TOKEN_ENCRYPTED","metadata":{"algorithm":"aes-256-gcm"}}When tokens become invalid or corrupted:
# Delete existing tokens
rm -f credentials/.gdrive-mcp-tokens.json
# Re-run authentication
node ./dist/index.js auth
# Or use the auth script
./scripts/auth.shNote: The current system supports one account per installation. For multiple accounts:
# Create separate installations
mkdir -p gdrive-account-1 gdrive-account-2
# Copy configuration to each
cp -r credentials .env gdrive-account-1/
cp -r credentials .env gdrive-account-2/
# Authenticate each separately
cd gdrive-account-1 && node ../dist/index.js auth
cd gdrive-account-2 && node ../dist/index.js authNote: Service accounts are not currently supported but can be added:
# Generate service account key (in Google Cloud Console)
# Download service-account-key.json
# Place in credentials directory
cp service-account-key.json credentials/
# Modify environment variables
echo "GDRIVE_USE_SERVICE_ACCOUNT=true" >> .env
echo "GDRIVE_SERVICE_ACCOUNT_PATH=credentials/service-account-key.json" >> .env# Standard local setup
source .env
node ./dist/index.js auth
# Development with watch mode
npm run watch &
node ./dist/index.js authImportant: Authentication must be done on the host machine, not inside Docker:
# Authenticate on host first
./scripts/auth.sh
# Verify tokens exist
ls -la credentials/.gdrive-mcp-tokens.json
# Then run Docker
docker-compose up -d
# Verify Docker container has access
docker-compose exec gdrive-mcp node dist/index.js health# Use production environment
export NODE_ENV=production
export LOG_LEVEL=error
# Run authentication with minimal logging
node ./dist/index.js auth
# Verify production health
node ./dist/index.js health# Continuous health monitoring
watch -n 300 "node ./dist/index.js health" # Every 5 minutes
# Monitor authentication events
tail -f logs/gdrive-mcp-audit.log | jq '.'
# Monitor general server logs
tail -f logs/combined.log | grep -E "(auth|token|error)"Status Meanings:
- 🟢 HEALTHY: Token valid, refresh capability available
- 🟡 DEGRADED: Token expiring soon, refresh in progress
- 🔴 UNHEALTHY: Token expired or no refresh capability
# Example health check responses
# HEALTHY
{
"status": "HEALTHY",
"checks": {
"tokenStatus": {
"status": "pass",
"message": "Token is valid and not expiring soon",
"metadata": {
"expiresIn": 2847,
"state": "AUTHENTICATED"
}
}
}
}
# DEGRADED
{
"status": "DEGRADED",
"checks": {
"tokenStatus": {
"status": "warn",
"message": "Token expiring soon, refresh in progress",
"metadata": {
"expiresIn": 298,
"state": "REFRESHING"
}
}
}
}
# UNHEALTHY
{
"status": "UNHEALTHY",
"checks": {
"tokenStatus": {
"status": "fail",
"message": "Token expired",
"metadata": {
"expiresIn": -1,
"state": "EXPIRED"
}
}
}
}Symptoms:
- Authentication command runs but no browser opens
- Terminal shows URL but no automatic browser launch
Solutions:
# Method 1: Copy URL manually
node ./dist/index.js auth
# Copy the displayed URL to your browser
# Method 2: Set browser explicitly
export BROWSER=/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
node ./dist/index.js auth
# Method 3: Use different browser
export BROWSER=firefox
node ./dist/index.js authSymptoms:
- Token refresh fails with "invalid_grant"
- Authentication works initially but fails later
Solutions:
# Check system clock synchronization
date
# Ensure time is correct
# Clear existing tokens and re-authenticate
rm -f credentials/.gdrive-mcp-tokens.json
node ./dist/index.js auth
# Verify OAuth app settings in Google Cloud Console
# Ensure "Desktop application" type is selected
# Check that OAuth consent screen is properly configuredSymptoms:
- Authentication fails immediately
- Error about missing or invalid client ID
Solutions:
# Verify OAuth keys file format
node -e "
const keys = JSON.parse(require('fs').readFileSync('credentials/gcp-oauth.keys.json'));
console.log('Keys structure:');
console.log('Has installed:', !!keys.installed);
console.log('Has web:', !!keys.web);
console.log('Client ID:', keys.installed?.client_id || keys.web?.client_id || 'NOT FOUND');
"
# Re-download OAuth credentials from Google Cloud Console
# Ensure "Desktop application" type was selected
# Replace the file and try again
cp /path/to/new/gcp-oauth.keys.json credentials/
node ./dist/index.js authSymptoms:
- Tokens can't be saved or loaded
- "Invalid key" or "Decryption failed" errors
Solutions:
# Check encryption key length
echo $GDRIVE_TOKEN_ENCRYPTION_KEY | base64 -d | wc -c
# Must output exactly: 32
# Regenerate encryption key if invalid
ENCRYPTION_KEY=$(openssl rand -base64 32)
echo "GDRIVE_TOKEN_ENCRYPTION_KEY=$ENCRYPTION_KEY" > .env
# Remove old encrypted tokens and re-authenticate
rm -f credentials/.gdrive-mcp-tokens.json
source .env
node ./dist/index.js authSymptoms:
- Authentication succeeds but API calls fail
- 403 Forbidden errors during operation
Solutions:
# Re-enable APIs with explicit project
gcloud config set project YOUR_PROJECT_ID
gcloud services enable drive.googleapis.com sheets.googleapis.com docs.googleapis.com forms.googleapis.com script.googleapis.com gmail.googleapis.com calendar-json.googleapis.com
# Wait 5-10 minutes for propagation
# Test with simple API call
node -e "
require('dotenv').config();
const {google} = require('googleapis');
const auth = new google.auth.GoogleAuth({scopes: ['https://www.googleapis.com/auth/drive.readonly']});
auth.getClient().then(client => console.log('Auth successful')).catch(console.error);
"Symptoms:
- OAuth consent shows different permissions than expected
- Some operations fail with permission errors
Solutions:
# Verify OAuth consent screen scopes
# Go to Google Cloud Console > APIs & Services > OAuth consent screen
# Ensure these exact scopes are added:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/documents
https://www.googleapis.com/auth/forms
https://www.googleapis.com/auth/script.projects.readonly
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.compose
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/calendar.readonly
https://www.googleapis.com/auth/calendar.events
# Clear tokens and re-authenticate with updated scopes
rm -f credentials/.gdrive-mcp-tokens.json
node ./dist/index.js authSymptoms:
- Initial authentication works but auto-refresh fails
- "No refresh token available" in logs
Solutions:
# Ensure OAuth app requests offline access
# This is configured in the application code
# If issue persists, revoke and re-authenticate:
# Revoke access in Google Account settings
# Go to myaccount.google.com/permissions
# Find "Google Drive MCP Server" and remove access
# Re-authenticate
rm -f credentials/.gdrive-mcp-tokens.json
node ./dist/index.js authSymptoms:
- Docker container can't access tokens
- Volume mount issues
Solutions:
# Ensure authentication is done on host, not in Docker
./scripts/auth.sh # Run on host machine
# Check volume mounts
docker-compose exec gdrive-mcp ls -la /credentials/
# Should show gcp-oauth.keys.json and .gdrive-mcp-tokens.json
# Check environment variables in container
docker-compose exec gdrive-mcp env | grep GDRIVE
# Should show encryption key and other settings
# Verify permissions
docker-compose exec gdrive-mcp stat /credentials/.gdrive-mcp-tokens.json
# Should be readable by container user- Never share
.gdrive-mcp-tokens.jsonfile - Never commit credentials to version control
- Regularly rotate encryption keys
- Monitor audit logs for suspicious activity
- Use secure permissions (600) for credential files
- Use "Internal" user type when possible for organizations
- Regularly review OAuth consent screen settings
- Monitor Google Cloud Console for unauthorized changes
- Enable 2FA on Google accounts used for authentication
- Audit token permissions periodically
- Secure
.envfiles with appropriate permissions - Use different encryption keys for different environments
- Monitor failed authentication attempts
- Implement log monitoring and alerting
- Regular backup and test recovery procedures
Once authentication is complete:
- Claude Desktop Integration - Connect to Claude
- Environment Variables Setup - Configure advanced settings
- Docker Deployment - Optional containerized deployment
Authentication Complete! ✅
Your Google Drive MCP Server is now authenticated and ready to use. The server will automatically manage token refresh and maintain authentication status.