kunstmaan/google-api-custom
Laravel/PHP wrapper for Google APIs with custom configuration support. Helps integrate Google services (e.g., analytics or other endpoints) using a simplified client setup, credential handling, and service initialization geared toward app-specific needs.
Install the Package:
composer require kunstmaan/google-api-custom
Publish the Configuration:
php artisan vendor:publish --provider="Kunstmaan\GoogleApiCustom\GoogleApiCustomServiceProvider"
This creates config/google-api-custom.php. Configure your Google API credentials (e.g., service account JSON key) and default scopes:
return [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'key_file' => base_path('path/to/credentials.json'),
'scopes' => [
'https://www.googleapis.com/auth/drive',
],
];
Register the Service Provider (if not auto-discovered):
Add to config/app.php under providers:
Kunstmaan\GoogleApiCustom\GoogleApiCustomServiceProvider::class,
First API Call (Google Drive Example): Use the facade or dependency injection:
// Option 1: Facade (if registered)
$files = \Google::service('Drive')->files()->listFiles();
// Option 2: Dependency Injection (recommended)
use Kunstmaan\GoogleApiCustom\GoogleClient;
class MyController {
public function __construct(private GoogleClient $googleClient) {}
public function index() {
$drive = $this->googleClient->service('Drive');
$files = $drive->files->listFiles();
return response()->json($files);
}
}
Verify Credentials: Test with a simple API call (e.g., list files) and check for auth errors. Use Laravel Tinker to debug:
php artisan tinker
>>> $drive = app(Kunstmaan\GoogleApiCustom\GoogleClient::class)->service('Drive');
>>> $drive->files->listFiles();
// In a service class
public function __construct(private GoogleClient $googleClient) {}
public function syncDriveFiles() {
$drive = $this->googleClient->service('Drive');
// Use $drive->files->* methods
}
config/google-api-custom.php):
'scopes' => env('APP_ENV') === 'local'
? ['https://www.googleapis.com/auth/drive.readonly']
: ['https://www.googleapis.com/auth/drive'],
.env for sensitive data:
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_secret
GOOGLE_KEY_FILE=/path/to/credentials.json
class GoogleDriveService {
public function __construct(private GoogleClient $client) {}
public function listFiles(string $query = '') {
return $this->client->service('Drive')->files->listFiles([
'q' => $query,
'fields' => 'files(id, name, mimeType)',
]);
}
}
// Testable with mocks
$service = new GoogleDriveService($mockGoogleClient);
try {
$response = $this->googleClient->service('Drive')->files->get('fileId');
} catch (\Google_Service_Exception $e) {
Log::error('Google API error: ' . $e->getMessage());
throw new \RuntimeException('Failed to fetch file', 0, $e);
}
config/app.php):
'aliases' => [
'Google' => Kunstmaan\GoogleApiCustom\Facades\Google::class,
],
$files = \Google::service('Drive')->files->listFiles();
namespace App\Http\Middleware;
use Closure;
use Kunstmaan\GoogleApiCustom\GoogleClient;
class ValidateGoogleAuth {
public function __construct(private GoogleClient $googleClient) {}
public function handle($request, Closure $next) {
$token = $request->bearerToken();
if (!$this->googleClient->verifyIdToken($token)) {
abort(401, 'Invalid Google token');
}
return $next($request);
}
}
$cacheKey = 'google_drive_files';
$files = Cache::remember($cacheKey, now()->addHours(1), function () {
return $this->googleClient->service('Drive')->files->listFiles();
});
GoogleClient in tests:
use Kunstmaan\GoogleApiCustom\GoogleClient;
use Mockery;
$mockClient = Mockery::mock(GoogleClient::class);
$mockClient->shouldReceive('service')
->with('Drive')
->andReturn($mockDriveService);
$service = new GoogleDriveService($mockClient);
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Kunstmaan\GoogleApiCustom\GoogleClient;
class SyncGoogleDrive extends Command {
protected $googleClient;
public function __construct(GoogleClient $googleClient) {
parent::__construct();
$this->googleClient = $googleClient;
}
public function handle() {
$files = $this->googleClient->service('Drive')->files->listFiles();
// Process files...
}
}
GoogleDriveSynced):
event(new GoogleDriveSynced($files));
Credential Leaks:
.env and never commit credentials.json to Git. Add it to .gitignore:
credentials.json
'key_file' => env('GOOGLE_KEY_FILE_PATH'),
Scope Mismatches:
403 Forbidden errors.config/google-api-custom.php and Google Cloud Console. Example for Drive:
'scopes' => [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.readonly',
],
PHP Version Conflicts:
array_merge signatures) despite fixes in the package.composer.json:
"config": {
"platform": {
"php": "8.2"
}
}
php -l or phpstan to catch deprecations early.Rate Limiting:
429 Too Many Requests) crashing your app.use GuzzleHttp\Exception\RequestException;
use Symfony\Component\HttpKernel\Exception\HttpException;
try {
$response = $this->googleClient->service('Drive')->files->listFiles();
} catch (RequestException $e) {
if ($e->getCode() === 429) {
sleep(2); // Retry after delay
return $this->handle();
}
throw new HttpException(500, 'Google API error');
}
Service Account vs. OAuth:
How can I help you explore Laravel packages today?