microsoft/microsoft-graph-core
Pros:
league/oauth2-client) and middleware (e.g., retry logic, logging), allowing TPMs to tailor it to Laravel’s ecosystem (e.g., integrating with Laravel’s HTTP client or caching layers).Cons:
microsoft/microsoft-graph (v1.0) or microsoft/microsoft-graph-beta for models/serialization, adding complexity. A TPM must decide whether to use raw JSON responses or invest in the full SDK.spatie/async) for synchronous workflows.socialiteproviders/microsoft) or custom implementations using league/oauth2-client.Illuminate\Support\Facades\Http) via middleware or by replacing Guzzle with Laravel’s client.429 Too Many Requests) must be mapped to Laravel’s exception system (e.g., HttpException).kiota (Microsoft’s codegen library), which may introduce breaking changes if Microsoft updates its Graph API schema.spatie/async or custom sync adapters) for synchronous codebases.monolog) to track Graph API calls and errors.spatie/circuit-breaker) for transient failures.laravel/sanctum or spatie/laravel-permission for token management..env files or a secrets manager (e.g., AWS Secrets Manager).microsoft/microsoft-graph SDK for models/serialization, or is raw JSON sufficient for your use case?Http facade by wrapping Guzzle or using middleware to adapt responses.socialiteproviders/microsoft) or custom implementations.socialite can be used as an alternative.Phase 1: Authentication Setup
// app/Providers/GraphServiceProvider.php
public function register()
{
$this->app->singleton(GraphClient::class, function ($app) {
$tenantId = config('services.microsoft.tenant_id');
$clientId = config('services.microsoft.client_id');
$clientSecret = config('services.microsoft.client_secret');
$tokenRequestContext = new ClientCredentialContext($tenantId, $clientId, $clientSecret);
$tokenProvider = new GraphPhpLeagueAccessTokenProvider($tokenRequestContext);
$guzzleConfig = [
'timeout' => 30,
'headers' => [
'Accept' => 'application/json',
],
];
$httpClient = GraphClientFactory::createWithConfig($guzzleConfig);
return new GraphClient($httpClient, $tokenProvider);
});
}
.env:
MICROSOFT_TENANT_ID=your_tenant_id
MICROSOFT_CLIENT_ID=your_client_id
MICROSOFT_CLIENT_SECRET=your_client_secret
Phase 2: HTTP Client Integration
Http facade:
// app/Services/GraphService.php
public function getUserProfile()
{
$token = $this->graphClient->getAccessToken();
return Http::withToken($token)->get('https://graph.microsoft.com/v1.0/me');
}
Phase 3: Token Management
// app/Console/Commands/RefreshMicrosoftGraphToken.php
public function handle()
{
$tokenProvider = app(GraphPhpLeagueAccessTokenProvider::class);
$tokenProvider->refreshToken();
}
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('graph:refresh-token')->hourly();
}
Phase 4: Error Handling and Observability
// app/Http/Middleware/HandleMicrosoftGraphErrors.php
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (GraphException $e) {
Log::error("Microsoft Graph Error: {$e->getMessage()}");
return response()->json(['error' => 'Microsoft Graph service unavailable'], 503);
}
}
How can I help you explore Laravel packages today?