43 lines
1.2 KiB
PHP
43 lines
1.2 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; // 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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|