Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Google Api Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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
    
  2. 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
    
  3. 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']);
    
  4. 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
    

Implementation Patterns

Workflows

  1. OAuth Authentication

    • Use the GoogleAuthController to handle redirects and callbacks.
    • Customize the 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');
      }
      
  2. Fetching Google API Data

    • Use the 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',
      ]);
      
  3. Middleware for Protected Routes

    • Protect routes requiring Google authentication:
      Route::middleware(['auth', 'google.auth'])->group(function () {
          // Protected routes
      });
      
  4. Customizing User Data

    • Override the GoogleUser model or its accessors to transform Google data:
      public function getFullNameAttribute()
      {
          return $this->names->first()->displayName ?? null;
      }
      

Integration Tips

  • Laravel Passport/ Sanctum: Combine with OAuth providers for seamless API authentication.
  • Service Providers: Bind custom Google API clients in AppServiceProvider:
    $this->app->bind('google.people', function () {
        return GoogleApi::people();
    });
    
  • Testing: Mock the Google API responses in tests:
    GoogleApi::shouldReceive('people()->get')
        ->once()
        ->andReturn($mockResponse);
    

Gotchas and Tips

Pitfalls

  1. Deprecated API Methods

    • The package was last updated in 2021. Ensure compatibility with Google’s latest API changes (e.g., OAuth scopes, endpoints).
    • Example: Some Google APIs may require updated scopes (e.g., https://www.googleapis.com/auth/userinfo.profile).
  2. Migration Issues

    • The 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();
      });
      
  3. State Management

    • The package may not handle CSRF protection for OAuth callbacks. Add it manually:
      Route::get('/login/google/callback', function () {
          if (!hash_equals((string) session('state'), (string) request()->query('state'))) {
              abort(403);
          }
          // Rest of the logic
      });
      
  4. Rate Limiting

    • Google APIs have strict rate limits. Cache responses aggressively:
      $userData = Cache::remember("google.user.{$user->google_id}", now()->addHours(1), function () {
          return GoogleApi::people()->get('people/me', [...]);
      });
      

Debugging Tips

  • Enable Debugging: Add this to .env to log Google API requests:
    GOOGLE_API_DEBUG=true
    
  • Check Logs: Inspect storage/logs/laravel.log for OAuth errors or API failures.
  • Test Locally: Use the Google OAuth Playground to validate scopes and tokens.

Extension Points

  1. Custom API Clients

    • Extend the 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;
      });
      
  2. Webhooks

    • Use Google’s Pub/Sub or other real-time APIs by extending the package’s event system.
  3. Multi-Tenancy

    • Store Google credentials per tenant in a tenants table and dynamically bind them:
      config(['google.client_id' => Tenant::current()->google_client_id]);
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky