progress
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?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; // 1 hour 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 {
|
||||
$response = Http::post('https://id.twitch.tv/oauth2/token', [
|
||||
'client_id' => config('services.igdb.client_id'),
|
||||
'client_secret' => config('services.igdb.client_secret'),
|
||||
'grant_type' => 'client_credentials',
|
||||
]);
|
||||
|
||||
if (!$response->successful()) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user