2025-04-10 00:44:55 +03:00
|
|
|
<?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';
|
2025-04-12 17:18:33 +03:00
|
|
|
private const TOKEN_EXPIRY = 3600*24*31; // 1 month in seconds
|
2025-04-10 00:44:55 +03:00
|
|
|
|
|
|
|
|
public function getAccessToken(): string
|
|
|
|
|
{
|
|
|
|
|
return Cache::remember(self::TOKEN_CACHE_KEY, self::TOKEN_EXPIRY, function () {
|
|
|
|
|
return $this->fetchNewAccessToken();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function fetchNewAccessToken(): string
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-04-12 17:18:33 +03:00
|
|
|
// 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,
|
2025-04-10 00:44:55 +03:00
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$response->successful()) {
|
2025-04-12 17:18:33 +03:00
|
|
|
Log::error('IGDB Auth Response', [
|
|
|
|
|
'status' => $response->status(),
|
|
|
|
|
'body' => $response->body()
|
|
|
|
|
]);
|
2025-04-10 00:44:55 +03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-12 17:18:33 +03:00
|
|
|
}
|