tomshaw/google-api
Laravel Google OAuth 2.0 service client with configurable token storage (DB or custom), published config, and migrations. Integrates google/apiclient-services and supports Composer cleanup to include only the Google APIs you need (e.g., Gmail, Calendar).
Install the package:
composer require tomshaw/google-api
Add this to composer.json to reduce bundle size:
"extra": {
"google/apiclient-services": ["Gmail", "Calendar"]
}
Publish config:
php artisan vendor:publish --provider="TomShaw\GoogleApi\Providers\GoogleApiServiceProvider" --tag=config
Configure config/google-api.php with:
auth_config: Path to your Google OAuth credentials JSON.service_scopes: Required scopes (e.g., ['https://www.googleapis.com/auth/calendar']).Run migrations (if using database storage):
php artisan migrate
use TomShaw\GoogleApi\GoogleClient;
// Redirect user to Google OAuth
$authUrl = $client->createAuthUrl();
return redirect($authUrl);
// Handle callback
$authCode = $request->get('code');
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
Use Laravel’s DI container to inject GoogleClient or service adapters:
public function __construct(
private GoogleClient $client,
private GoogleCalendar $calendar
) {}
Leverage built-in adapters for common APIs:
// Calendar
$events = $calendar->listEvents();
$event = $calendar->createEvent(['summary' => 'Meeting']);
// Gmail (with Laravel Mailable)
$mailable = new SendGmailMail($user);
$gmail = new GoogleGmail($client);
$gmail->send($mailable);
$client->refreshAccessToken();
StorageAdapterInterface for Redis/Redis:
'token_storage_adapter' => App\Services\RedisTokenStorage::class,
public function syncCalendar(GoogleCalendar $calendar) {
$events = $calendar->listEvents();
foreach ($events as $event) {
Event::updateOrCreate(
['google_id' => $event->id],
['title' => $event->summary]
);
}
}
service_scopes in config matches the API’s requirements. Missing scopes cause 403 errors.Google_Auth_Exception for expired tokens:
try {
$client->refreshAccessToken();
} catch (\Google_Auth_Exception $e) {
// Redirect to OAuth flow
}
composer.json to avoid bloating the vendor directory.$client->setDeveloperKey('YOUR_KEY'); // For API calls
$client->setLogLevel(\Google_Client::LOG_DEBUG);
token_storage_adapter is correctly configured (e.g., database/Redis).GoogleApiService for unsupported APIs:
class GoogleSheets extends GoogleApiService {
protected $serviceName = 'sheets';
// Add custom methods
}
public function handle(Request $request, Closure $next) {
if (!$request->user()->hasGoogleToken()) {
return redirect()->route('google.auth');
}
return $next($request);
}
Google_Service_Resource methods for bulk operations (e.g., users.batchGet in Admin SDK).$events = Cache::remember("google_events_{$userId}", now()->addHours(1), function() use ($calendar) {
return $calendar->listEvents();
});
$drive = app(GoogleDrive::class)->onlyIf(function() {
return request()->has('drive_action');
});
How can I help you explore Laravel packages today?