Files
cleargame/app/Services/IgdbAuthService.php
2025-04-12 17:18:33 +03:00

61 lines
1.9 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class IgdbAuthService
{
private const TOKEN_CACHE_KEY = 'igdb_access_token';
private const TOKEN_EXPIRY = 3600*24*31; // 1 month in seconds
public function getAccessToken(): string
{
return Cache::remember(self::TOKEN_CACHE_KEY, self::TOKEN_EXPIRY, function () {
return $this->fetchNewAccessToken();
});
}
private function fetchNewAccessToken(): string
{
try {
// Log the config values to debug
Log::info('IGDB Auth Attempt', [
'client_id' => config('services.igdb.credentials.client_id'),
'client_secret' => config('services.igdb.credentials.client_secret')
]);
$clientId = config('igdb.credentials.client_id');
$clientSecret = config('igdb.credentials.client_secret');
if (empty($clientId) || empty($clientSecret)) {
throw new \Exception("IGDB credentials are not properly configured");
}
$response = Http::asForm()->post('https://id.twitch.tv/oauth2/token', [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
]);
if (!$response->successful()) {
Log::error('IGDB Auth Response', [
'status' => $response->status(),
'body' => $response->body()
]);
throw new \Exception("Failed to get access token: " . $response->body());
}
$data = $response->json();
return $data['access_token'];
} catch (\Exception $e) {
Log::error('Failed to fetch IGDB access token', [
'error' => $e->getMessage()
]);
throw $e;
}
}
}