How Do I Connect Laravel Socialite with Google Client PHP Library?
Integrating Google login into your Laravel application boosts user experience and opens up powerful features like accessing calendars, Gmail, and Drive. Laravel Socialite simplifies OAuth logins, but if you want more than just authentication, combining it with the Google Client PHP Library gives you access to the full potential of Google’s API ecosystem. This guide walks you through the integration process step by step.
The Problem: Limited OAuth Integration
Laravel Socialite handles basic Google OAuth authentication well. However, it falls short when developers need to access advanced Google services like reading a user’s calendar, accessing Gmail threads, or syncing Drive files. Without deeper API support, your app’s integration remains shallow and restricted to basic login functionality.
The Solution: Dual Integration Setup
To overcome these limitations, integrate both Laravel Socialite and the Google Client PHP Library. This combination allows you to authenticate users with Socialite and then use Google Client to access extended API scopes and services.
Step-by-Step Guide:
- Install Required Packages
Run this Composer command in your Laravel project:
1 | composer require laravel/socialite google/apiclient |
- Set Up Google OAuth Credentials
- Visit Google Developer Console
- Create a new project
- Enable OAuth 2.0 and set up the consent screen
- Add required scopes
- Generate Client ID and Secret
- Configure Environment File (.env)
1 2 3 4 5 | GOOGLE_CLIENT_ID=your-client-id GOOGLE_CLIENT_SECRET=your-client-secret GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/google/callback |
- Update config/services.php
1 2 3 4 5 6 7 8 9 | 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('GOOGLE_REDIRECT_URI'), ], |
- Create Authentication Controller
In GoogleAuthController:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | use Laravel\Socialite\Facades\Socialite; use Google_Client; public function redirectToGoogle() { return Socialite::driver('google')->redirect(); } public function handleGoogleCallback() { $user = Socialite::driver('google')->stateless()->user(); // Save user data or login the user here return redirect('/home'); } |
- Add Routes
1 2 3 | Route::get('auth/google', [GoogleAuthController::class, 'redirectToGoogle']); Route::get('auth/google/callback', [GoogleAuthController::class, 'handleGoogleCallback']); |
- Extend Functionality with Google Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public function getGoogleClient() { $client = new Google_Client(); $client->setClientId(env('GOOGLE_CLIENT_ID')); $client->setClientSecret(env('GOOGLE_CLIENT_SECRET')); $client->setRedirectUri(env('GOOGLE_REDIRECT_URI')); $client->addScope('https://www.googleapis.com/auth/calendar.readonly'); return $client; } |
Conclusion
By combining Laravel Socialite with the Google Client PHP Library, you’re no longer limited to basic OAuth authentication. This setup gives you access to Google’s advanced APIs, enabling feature-rich experiences for your users. Need help implementing this or customizing your Laravel app?
Explore Our Laravel Services to build smarter, scalable, and Google-integrated web solutions.
Recent help desk articles

Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
