tipoff/laravel-google-api
Laravel integration for Google APIs using OAuth client secrets and stored access tokens. Publishes config, reads .env settings, supports service-specific overrides, and includes GMB Account and Key models with built-in policies and Nova resources.
Installation
composer require tipoff/laravel-google-api
Publish the config and migration:
php artisan vendor:publish --provider="Tipoff\GoogleApi\GoogleApiServiceProvider" --tag="config"
php artisan vendor:publish --provider="Tipoff\GoogleApi\GoogleApiServiceProvider" --tag="migrations"
php artisan migrate
Configure .env
Add Google API credentials (Client ID, Client Secret, Redirect URI) to .env:
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REDIRECT_URI=http://your-app.test/login/google/callback
First Use Case: OAuth Login
Add the Google login route to routes/web.php:
Route::get('/login/google', [GoogleAuthController::class, 'redirectToGoogle'])->name('google.login');
Route::get('/login/google/callback', [GoogleAuthController::class, 'handleGoogleCallback']);
Key Model
The package provides a GoogleUser model (or similar) to store user data fetched from Google. Extend it if needed:
php artisan make:model GoogleUser -m
OAuth Authentication
GoogleAuthController to handle redirects and callbacks.handleGoogleCallback method to map Google user data to your Laravel user model:
public function handleGoogleCallback()
{
$googleUser = GoogleAuth::handleCallback();
$user = User::firstOrCreate([
'email' => $googleUser->email,
], [
'name' => $googleUser->name,
'google_id' => $googleUser->id,
]);
auth()->login($user);
return redirect('/home');
}
Fetching Google API Data
GoogleApi facade to interact with Google APIs (e.g., People API, Drive API):
use Tipoff\GoogleApi\Facades\GoogleApi;
$userData = GoogleApi::people()->get('people/me', [
'personFields' => 'names,emailAddresses,photos',
]);
Middleware for Protected Routes
Route::middleware(['auth', 'google.auth'])->group(function () {
// Protected routes
});
Customizing User Data
GoogleUser model or its accessors to transform Google data:
public function getFullNameAttribute()
{
return $this->names->first()->displayName ?? null;
}
AppServiceProvider:
$this->app->bind('google.people', function () {
return GoogleApi::people();
});
GoogleApi::shouldReceive('people()->get')
->once()
->andReturn($mockResponse);
Deprecated API Methods
https://www.googleapis.com/auth/userinfo.profile).Migration Issues
google_users table migration might not match your needs. Customize it or create a new migration:
Schema::create('google_users', function (Blueprint $table) {
$table->id();
$table->string('google_id')->unique();
$table->json('raw_data'); // Store full Google response if needed
$table->timestamps();
});
State Management
Route::get('/login/google/callback', function () {
if (!hash_equals((string) session('state'), (string) request()->query('state'))) {
abort(403);
}
// Rest of the logic
});
Rate Limiting
$userData = Cache::remember("google.user.{$user->google_id}", now()->addHours(1), function () {
return GoogleApi::people()->get('people/me', [...]);
});
.env to log Google API requests:
GOOGLE_API_DEBUG=true
storage/logs/laravel.log for OAuth errors or API failures.Custom API Clients
GoogleApi facade to support additional APIs (e.g., Calendar, Gmail):
// In AppServiceProvider
$this->app->extend('google.api', function ($api) {
$api->calendar = $api->service('calendar');
return $api;
});
Webhooks
Multi-Tenancy
tenants table and dynamically bind them:
config(['google.client_id' => Tenant::current()->google_client_id]);
How can I help you explore Laravel packages today?