- How do I install **google/apiclient** in a Laravel project?
- Run `composer require google/apiclient` in your Laravel project root. The package supports PHP 8.0+ and integrates with Laravel’s Composer dependency system. For large projects, consider running `composer install --optimize-autoloader` to reduce bundle size.
- Which Laravel versions are compatible with **google/apiclient**?
- The package requires PHP 8.0+, so it works with Laravel 8.x, 9.x, and 10.x. Older Laravel versions (7.x or below) may need PHP upgrades or manual adjustments for compatibility.
- How do I authenticate with OAuth 2.0 in Laravel using this package?
- Register the `Google_Client` as a singleton in `AppServiceProvider` and configure OAuth credentials via `setClientId()`, `setClientSecret()`, and `setRedirectUri()`. Use Laravel’s session or redirect helpers to handle OAuth callbacks securely.
- Can I use service accounts instead of OAuth for server-to-server API calls?
- Yes. Load your service account JSON key file via `setAuthConfig()` and set the scope (e.g., `Drive::DRIVE`). Store credentials securely in Laravel’s encrypted storage or environment variables.
- How do I reduce the package size if I only need a few Google APIs?
- Run `composer require google/apiclient --ignore-platform-reqs` and manually remove unused API service classes from `vendor/google/apiclient/src/`. Alternatively, use `composer cleanup` to optimize autoloading.
- Is there a Laravel facade or helper to simplify API calls?
- No built-in facade, but you can create one in `app/Facades/GoogleApi.php` to wrap `Google_Client` methods. Example: `GoogleApi::getDriveService()->files()->listFiles()`. This abstracts initialization logic.
- How do I handle token refreshes in Laravel?
- Set a token refresh callback in `Google_Client` (e.g., `setAccessToken()`) and log new tokens to Laravel’s database or cache. For async retries, dispatch a job using Laravel Queues.
- Can I cache API responses in Laravel?
- Yes. Use Laravel’s cache drivers (Redis, file) with PSR-6 adapters. Example: Cache a Drive file list with `Cache::put('drive_files', $response, now()->addHours(1))`. The package supports PSR-6 caching natively.
- What’s the difference between **google/apiclient** and **google/cloud-*** libraries?
- **google/apiclient** is a monolithic library for all Google APIs (e.g., Gmail, YouTube) but is in maintenance mode. For Google Cloud Platform APIs (e.g., Cloud Storage), use dedicated **google/cloud-*** libraries, which are actively developed.
- How do I secure API credentials in production?
- Store JSON key files in Laravel’s encrypted storage (`storage/app/google_credentials.json`) or use environment variables. For serverless environments, set `GOOGLE_APPLICATION_CREDENTIALS` to the path of your credentials file.