Installation
composer require beyerz/google-api-bundle
Add the bundle to config/bundles.php:
return [
Beyerz\GoogleApiBundle\BeyerzGoogleApiBundle::class => ['all' => true],
];
Configuration
Place your client_secret.json in config/packages/beyerz_google_api/client_secret.json (or adjust path in config).
Configure config/packages/beyerz_google_api.yaml:
beyerz_google_api:
application_name: 'MyApp'
client_secret_path: '%kernel.project_dir%/config/packages/beyerz_google_api/client_secret.json'
scopes:
- 'https://www.googleapis.com/auth/userinfo.email'
First Use Case
Inject the GoogleApiService in a controller/service:
use Beyerz\GoogleApiBundle\Service\GoogleApiService;
class MyController extends AbstractController {
public function __construct(private GoogleApiService $googleApi) {}
public function index() {
$userInfo = $this->googleApi->getUserInfo();
return $this->json($userInfo);
}
}
OAuth Flow Redirect users to Google OAuth:
$authUrl = $this->googleApi->getAuthorizationUrl();
return $this->redirect($authUrl);
Handle callback:
$token = $this->googleApi->handleAuthCallback($request);
$this->googleApi->setAccessToken($token);
API Calls Use the service to interact with Google APIs:
// Gmail example
$messages = $this->googleApi->getGmailService()->users_messages->listUsersMessages('me');
// People API example
$contacts = $this->googleApi->getPeopleService()->people->connections->listConnections('people/me');
Service Management Access specific Google services:
$driveService = $this->googleApi->getDriveService();
$files = $driveService->files->listFiles();
Token Refresh Automatically handle token refresh:
try {
$data = $this->googleApi->getUserInfo();
} catch (Exception $e) {
$this->googleApi->refreshAccessToken();
$data = $this->googleApi->getUserInfo();
}
Dependency Injection
Prefer constructor injection for GoogleApiService to ensure proper lifecycle management.
Configuration Overrides
Override bundle config per environment (e.g., config/packages/dev/beyerz_google_api.yaml).
Event Listeners Listen for token refresh events:
$eventDispatcher->addListener(
GoogleApiEvents::TOKEN_REFRESHED,
function (TokenRefreshedEvent $event) {
// Log or store the new token
}
);
Custom Services
Extend the bundle’s GoogleApiService for project-specific logic:
class CustomGoogleApiService extends GoogleApiService {
public function customMethod() {
// Custom logic
}
}
Token Storage
CredentialsManager or use Symfony’s security.token_storage for session-based storage.beyerz_google_api:
credentials_manager: 'Beyerz\GoogleApiBundle\Manager\SessionCredentialsManager'
Scope Management
scopes config includes all required permissions for your use case.Google_Auth_Exception.Service Initialization
Gmail, Drive) are lazy-loaded. Accessing them before authentication will fail.$this->googleApi->isAuthorized() before making API calls.Deprecated PHP Version
Enable Debug Logging
Add to config/packages/monolog.yaml:
handlers:
google_api:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.google_api.log"
level: debug
Check Token Validity
Use the isAccessTokenExpired() method to debug token issues:
if ($this->googleApi->isAccessTokenExpired()) {
$this->googleApi->refreshAccessToken();
}
Common Exceptions
Google_Service_Exception: API-specific errors (check response details).Google_Auth_Exception: Token/auth issues (validate scopes and credentials).Custom Credentials Manager
Extend CredentialsManager to integrate with your auth system:
class MyCredentialsManager extends CredentialsManager {
public function getAccessToken() {
// Fetch from your storage (DB, cache, etc.)
}
}
Add New Google Services
Register additional services in services.yaml:
services:
Beyerz\GoogleApiBundle\Service\GoogleApiService:
arguments:
$services: ['Gmail', 'Drive', 'People'] # Add your service names
Override Service Factories Replace the default service factory for custom behavior:
$factory = new CustomGoogleServiceFactory($client);
$this->googleApi->setServiceFactory($factory);
Event-Driven Extensions Listen for bundle events to extend functionality:
$eventDispatcher->addListener(
GoogleApiEvents::SERVICE_INITIALIZED,
function (ServiceInitializedEvent $event) {
$event->getService()->setUseBatch(true); // Example: Enable batch mode
}
);
Client Secret Path
Use absolute paths or Symfony’s %kernel.project_dir% placeholder for portability.
Scopes Duplication
The README includes duplicate scopes (e.g., plus.login appears twice). Clean up your config to avoid unnecessary requests.
Service Caching Google services are instantiated once per request. Clear caches if modifying the bundle’s service configuration dynamically.
How can I help you explore Laravel packages today?