revolution/laravel-google-sheets
Service Account authentication is ideal for server-to-server access to Google Sheets that your application owns or has been granted access to. This method doesn't require user interaction and is perfect for automated systems, background jobs, or applications that need to access specific spreadsheets programmatically.
storage/app/ directorygoogle-service-account.json.gitignore to prevent committing credentials to version control# Add to .gitignore
storage/app/google-service-account.json
Add the following to your .env file:
GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=storage/app/google-service-account.json
Important: Ensure your config/google.php includes the required scopes setting for both OAuth and Service Account authentication:
'scopes' => [
\Google\Service\Sheets::SPREADSHEETS,
\Google\Service\Drive::DRIVE,
],
For each Google Sheets spreadsheet you want to access:
client_email fieldExample service account email format:
your-service-account@your-project-id.iam.gserviceaccount.com
Once configured, you can use the service account authentication seamlessly:
use Revolution\Google\Sheets\Facades\Sheets;
// Service account authentication is automatically handled
// No need to set access tokens manually
$values = Sheets::spreadsheet('your-spreadsheet-id')
->sheet('Sheet1')
->all();
// Or using spreadsheet title (requires Drive API access)
$values = Sheets::spreadsheetByTitle('My Spreadsheet')
->sheet('Sheet1')
->all();
For production environments, consider these additional security measures:
| Variable | Description | Example |
|---|---|---|
GOOGLE_SERVICE_ENABLED |
Enable service account authentication | true |
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION |
Path to service account JSON file | storage/app/google-service-account.json |
"The caller does not have permission" error
"File not found" error
"API not enabled" error
"Invalid credentials" error
Create a simple test route to verify your service account setup:
// routes/web.php
Route::get('/test-sheets', function () {
try {
$sheets = Sheets::spreadsheetList();
return response()->json([
'status' => 'success',
'message' => 'Service account authentication working',
'spreadsheet_count' => count($sheets)
]);
} catch (\Exception $e) {
return response()->json([
'status' => 'error',
'message' => $e->getMessage()
]);
}
});
You can store the service account credentials as a JSON string in your environment variable and decode it in your configuration file. This method is particularly well-suited for deployment scenarios like GitHub Actions, as it allows the entire service account credentials to be stored as a single secret.
Step 1: Add JSON string to your .env file:
GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION='{"type": "service_account", "project_id": "your-project-id", "private_key_id": "your-private-key-id", "private_key": "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY_HERE\n-----END PRIVATE KEY-----\n", "client_email": "your-service-account@your-project-id.iam.gserviceaccount.com", "client_id": "your-client-id", "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/oauth2/v1/certs/your-service-account%40your-project-id.iam.gserviceaccount.com"}'
Step 2: Update your config/google.php to decode the JSON string:
// config/google.php
'service' => [
'enable' => env('GOOGLE_SERVICE_ENABLED', false),
'file' => json_decode(env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', ''), true),
],
This approach eliminates the need to store a separate JSON file and makes deployment easier, especially in containerized environments or CI/CD pipelines.
How can I help you explore Laravel packages today?