When to Use Service Account
- Server-to-server access — Your application accesses Google Sheets without user interaction
- Automated systems — Background jobs, cron tasks, automated reporting
- Fixed spreadsheet access — Working with specific spreadsheets you control
- Production applications — Reliable authentication without user consent flows
Prerequisites
- Google Cloud Console project
- Google Sheets API and Google Drive API enabled
- Project admin access to create Service Accounts
Setup Steps
Create a Google Cloud Project
- Go to Google Cloud Console
- Create a new project or select an existing one
- Note your project ID for later use
Enable Required APIs
- Navigate to APIs & Services > Library
- Search for and enable:
- Google Sheets API
- Google Drive API
Create a Service Account
- Go to APIs & Services > Credentials
- Click Create Credentials > Service Account
- Fill in the details:
- Service account name: Descriptive name (e.g., “Laravel Sheets App”)
- Service account ID: Auto-generated
- Description: Optional
- Click Create and Continue
- Skip optional sections and click Done
Generate Service Account Key
- In the Credentials page, click on your Service Account
- Go to the Keys tab
- Click Add Key > Create new key
- Choose JSON as the key type
- Click Create
- The JSON file will download automatically
Store the JSON File
- Move the downloaded JSON to your
storage/app/directory - Rename it to
google-service-account.json - Important: Add to
.gitignoreto exclude from version control
Share Spreadsheets with Service Account
For each Google Sheet you want to access:
- Open the spreadsheet
- Click Share
- Get the
client_emailfrom your JSON file - Share the sheet with this email (format:
[email protected]) - Grant permissions:
- Viewer — Read-only
- Editor — Read and write
- Owner — Full access (not recommended)
Usage
Once configured, Service Account authentication works automatically:Security Best Practices
1. Limit Service Account Permissions
- Only grant access to necessary spreadsheets
- Use Editor instead of Owner when possible
- Regularly audit which spreadsheets have access
2. Secure Key Storage
- Never commit service account keys to version control
- Use environment variables for key file paths
- Store keys outside the web root in production
- Rotate keys periodically
3. Production Deployment
Additional security measures:- Store JSON key in a secure location outside web root
- Use separate Service Accounts for different environments (dev, staging, production)
- Monitor account usage through Google Cloud Console
- Set up logging and alerts for suspicious activity
Environment Variables Reference
| Variable | Description | Example |
|---|---|---|
GOOGLE_SERVICE_ENABLED | Enable service account auth | true |
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION | Path to JSON key file | storage/app/google-service-account.json |
Advanced Configuration
Using JSON String in Environment Variable
Store the service account credentials as a JSON string instead of a file, ideal for CI/CD environments like GitHub Actions. Step 1: Store JSON string in .envTroubleshooting
Common Errors
“caller does not have permission” error- Verify the spreadsheet is shared with the Service Account email
- Ensure permissions are Editor or higher for write operations
- Check the JSON file path is correct
- Ensure the file exists and is readable by the web server
- Verify Google Sheets API and Google Drive API are enabled
- Wait a few minutes for changes to propagate
- Verify the JSON key file is valid and not corrupted
- Ensure the Service Account hasn’t been deleted or disabled
- Check that the project ID matches your Google Cloud project