Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
refactor(social): single source of truth for token refresh
Paulo Castellanofcb70b3599f2
diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.phpindex 68c5fc153..bc7a5b65f 100644--- a/app/Services/Social/BlueskyPublisher.php+++ b/app/Services/Social/BlueskyPublisher.php@@ -6,7 +6,6 @@ namespace App\Services\Social;use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\BlueskyPublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Media\MediaOptimizer;@@ -30,7 +29,7 @@ class BlueskyPublisher// Refresh token if neededif ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -268,50 +267,6 @@ class BlueskyPublisherreturn "https://bsky.app/profile/{$handle}/post/{$postId}";}- public function refreshToken(SocialAccount $account): void- {- $service = $account->meta['service'] ?? config('trypost.platforms.bluesky.default_service');- $client = TokenRefreshClient::for(Platform::Bluesky);-- try {- $response = $client->send(fn () => $this->socialHttp()->withToken($account->refresh_token)- ->post("{$service}/xrpc/com.atproto.server.refreshSession"));-- $data = $response->json();- $account->update([- 'access_token' => data_get($data, 'accessJwt'),- 'refresh_token' => data_get($data, 'refreshJwt'),- 'token_expires_at' => now()->addHours(2),- ]);-- return;- } catch (TokenExpiredException) {- // refresh token rejected (4xx) — fall back to re-auth below- }-- if (isset($account->meta['password'])) {- try {- $reauth = $client->send(fn () => Http::post("{$service}/xrpc/com.atproto.server.createSession", [- 'identifier' => $account->meta['identifier'],- 'password' => decrypt($account->meta['password']),- ]));-- $data = $reauth->json();- $account->update([- 'access_token' => data_get($data, 'accessJwt'),- 'refresh_token' => data_get($data, 'refreshJwt'),- 'token_expires_at' => now()->addHours(2),- ]);-- return;- } catch (TokenExpiredException) {- // re-auth rejected with stored credentials — fall through- }- }-- throw new TokenExpiredException('Bluesky session expired');- }-private function handleApiError(Response $response): never{throw BlueskyPublishException::fromApiResponse($response);diff --git a/app/Services/Social/Concerns/HasSocialHttpClient.php b/app/Services/Social/Concerns/HasSocialHttpClient.phpindex 301032a28..1aa41c7c2 100644--- a/app/Services/Social/Concerns/HasSocialHttpClient.php+++ b/app/Services/Social/Concerns/HasSocialHttpClient.php@@ -5,9 +5,7 @@ declare(strict_types=1);namespace App\Services\Social\Concerns;use App\Models\PostPlatform;-use App\Models\SocialAccount;use Illuminate\Http\Client\PendingRequest;-use Illuminate\Support\Facades\Cache;use Illuminate\Support\Facades\Http;trait HasSocialHttpClient@@ -28,25 +26,6 @@ trait HasSocialHttpClient);}- protected function refreshTokenWithLock(SocialAccount $account, callable $refreshFn): void- {- $lock = Cache::lock("token_refresh:{$account->id}", 30);-- if (! $lock->get()) {- // Another process is refreshing, wait and reload- sleep(2);- $account->refresh();-- return;- }-- try {- $refreshFn();- } finally {- $lock->release();- }- }-protected function socialHttp(): PendingRequest{return Http::retry(diff --git a/app/Services/Social/ConnectionVerifier.php b/app/Services/Social/ConnectionVerifier.phpindex d13bb6200..a0b25dfb1 100644--- a/app/Services/Social/ConnectionVerifier.php+++ b/app/Services/Social/ConnectionVerifier.php@@ -112,10 +112,10 @@ class ConnectionVerifierprivate function refreshLinkedInToken(SocialAccount $account): void{if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for LinkedIn account');+ throw new TokenExpiredException("No refresh token available for {$account->platform->label()} account");}- $response = TokenRefreshClient::for(Platform::LinkedIn)->send(fn () => Http::asForm()+ $response = TokenRefreshClient::for($account->platform)->send(fn () => Http::asForm()->post(config('trypost.platforms.linkedin.oauth_api').'/oauth/v2/accessToken', ['grant_type' => 'refresh_token','refresh_token' => $account->refresh_token,diff --git a/app/Services/Social/InstagramAnalytics.php b/app/Services/Social/InstagramAnalytics.phpindex 126a4626a..de677310e 100644--- a/app/Services/Social/InstagramAnalytics.php+++ b/app/Services/Social/InstagramAnalytics.php@@ -5,15 +5,12 @@ declare(strict_types=1);namespace App\Services\Social;use App\Enums\PostPlatform\ContentType;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Carbon\CarbonInterface;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class InstagramAnalytics@@ -48,7 +45,7 @@ class InstagramAnalytics$this->baseUrl = $account->platform->instagramGraphBaseUrl();if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -92,7 +89,7 @@ class InstagramAnalytics$this->baseUrl = $account->platform->instagramGraphBaseUrl();if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -200,27 +197,4 @@ class InstagramAnalytics{return $this->socialHttp();}-- private function refreshToken(SocialAccount $account): void- {- if ($account->platform === Platform::InstagramFacebook) {- return;- }-- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for Instagram account');- }-- $response = TokenRefreshClient::for(Platform::Instagram)->send(fn () => Http::get(config('trypost.platforms.instagram.auth_api').'/refresh_access_token', [- 'grant_type' => 'ig_refresh_token',- 'access_token' => $account->access_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.phpindex 9ad27dd5e..d4da82740 100644--- a/app/Services/Social/InstagramPublisher.php+++ b/app/Services/Social/InstagramPublisher.php@@ -5,11 +5,9 @@ declare(strict_types=1);namespace App\Services\Social;use App\Enums\PostPlatform\ContentType;-use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\InstagramPublishException;use App\Models\PostPlatform;-use App\Models\SocialAccount;use App\Services\Media\MediaOptimizer;use App\Services\Social\Concerns\HasSocialHttpClient;use Illuminate\Http\Client\Response;@@ -32,7 +30,7 @@ class InstagramPublisher$this->baseUrl = $account->platform->instagramGraphBaseUrl();if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -395,29 +393,6 @@ class InstagramPublisherLog::warning('Instagram media processing timeout, proceeding anyway');}- private function refreshToken(SocialAccount $account): void- {- // Instagram via Facebook uses page tokens that don't expire- if ($account->platform === Platform::InstagramFacebook) {- return;- }-- $response = TokenRefreshClient::for(Platform::Instagram)->send(fn () => Http::get(config('trypost.platforms.instagram.auth_api').'/refresh_access_token', [- 'grant_type' => 'ig_refresh_token',- 'access_token' => $account->access_token,- ]));-- $data = $response->json();- $newToken = data_get($data, 'access_token');-- $account->update([- 'access_token' => $newToken,- 'refresh_token' => $newToken,- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);-- }-private function handleApiError(Response $response): never{throw InstagramPublishException::fromApiResponse($response);diff --git a/app/Services/Social/LinkedInPageAnalytics.php b/app/Services/Social/LinkedInPageAnalytics.phpindex 98c1d5197..3fdba5499 100644--- a/app/Services/Social/LinkedInPageAnalytics.php+++ b/app/Services/Social/LinkedInPageAnalytics.php@@ -4,15 +4,12 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Carbon\CarbonInterface;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class LinkedInPageAnalytics@@ -54,7 +51,7 @@ class LinkedInPageAnalytics}if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -84,7 +81,7 @@ class LinkedInPageAnalyticsprivate function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -234,27 +231,4 @@ class LinkedInPageAnalytics'X-Restli-Protocol-Version' => '2.0.0',]);}-- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for LinkedIn Page account');- }-- $response = TokenRefreshClient::for(Platform::LinkedInPage)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.linkedin.oauth_api').'/oauth/v2/accessToken', [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- 'client_id' => config('services.linkedin-openid.client_id'),- 'client_secret' => config('services.linkedin-openid.client_secret'),- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.phpindex 7486f5d35..8269a21b5 100644--- a/app/Services/Social/LinkedInPagePublisher.php+++ b/app/Services/Social/LinkedInPagePublisher.php@@ -46,7 +46,7 @@ class LinkedInPagePublisher$this->hasRetried = false;if ($this->account->is_token_expired || $this->account->is_token_expiring_soon) {- $this->refreshTokenWithLock($this->account, fn () => $this->refreshToken($this->account));+ app(ConnectionVerifier::class)->refreshToken($this->account);$this->account->refresh();}@@ -81,7 +81,7 @@ class LinkedInPagePublisher$this->hasRetried = true;try {- $this->refreshToken($this->account);+ app(ConnectionVerifier::class)->refreshToken($this->account);$this->account->refresh();$this->accessToken = $this->account->access_token;@@ -452,32 +452,6 @@ class LinkedInPagePublisherLog::warning('LinkedIn Page video processing timeout, proceeding anyway');}- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for LinkedIn Page account');- }-- $response = TokenRefreshClient::for(Platform::LinkedInPage)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.linkedin.oauth_api').'/oauth/v2/accessToken', [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- 'client_id' => config('services.linkedin-openid.client_id'),- 'client_secret' => config('services.linkedin-openid.client_secret'),- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);-- // Sync tokens to LinkedIn personal if it exists- app(LinkedInTokenSynchronizer::class)->syncTokens($account);- }-private function handleApiError(Response $response): never{throw LinkedInPublishException::fromApiResponse($response);diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.phpindex ed6d230c6..567bf1e03 100644--- a/app/Services/Social/LinkedInPublisher.php+++ b/app/Services/Social/LinkedInPublisher.php@@ -46,7 +46,7 @@ class LinkedInPublisher$this->hasRetried = false;if ($this->account->is_token_expired || $this->account->is_token_expiring_soon) {- $this->refreshTokenWithLock($this->account, fn () => $this->refreshToken($this->account));+ app(ConnectionVerifier::class)->refreshToken($this->account);$this->account->refresh();}@@ -75,7 +75,7 @@ class LinkedInPublisher$this->hasRetried = true;try {- $this->refreshToken($this->account);+ app(ConnectionVerifier::class)->refreshToken($this->account);$this->account->refresh();$this->accessToken = $this->account->access_token;@@ -433,32 +433,6 @@ class LinkedInPublisherLog::warning('LinkedIn video processing timeout, proceeding anyway');}- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for LinkedIn account');- }-- $response = TokenRefreshClient::for(Platform::LinkedIn)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.linkedin.oauth_api').'/oauth/v2/accessToken', [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- 'client_id' => config('services.linkedin.client_id'),- 'client_secret' => config('services.linkedin.client_secret'),- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);-- // Sync tokens to LinkedIn Page if it exists- app(LinkedInTokenSynchronizer::class)->syncTokens($account);- }-private function handleApiError(Response $response): never{throw LinkedInPublishException::fromApiResponse($response);diff --git a/app/Services/Social/PinterestAnalytics.php b/app/Services/Social/PinterestAnalytics.phpindex 2a343b9ea..d18391a43 100644--- a/app/Services/Social/PinterestAnalytics.php+++ b/app/Services/Social/PinterestAnalytics.php@@ -4,15 +4,12 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Carbon\CarbonInterface;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class PinterestAnalytics@@ -50,7 +47,7 @@ class PinterestAnalytics}if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -95,7 +92,7 @@ class PinterestAnalyticsprivate function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -161,27 +158,4 @@ class PinterestAnalytics{return $this->socialHttp()->withToken($this->accessToken);}-- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for Pinterest account');- }-- $response = TokenRefreshClient::for(Platform::Pinterest)->send(fn () => Http::withBasicAuth(- config('services.pinterest.client_id'),- config('services.pinterest.client_secret'),- )->asForm()->post(config('trypost.platforms.pinterest.api').'/oauth/token', [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.phpindex 116b93f5e..5fe1d7b33 100644--- a/app/Services/Social/PinterestPublisher.php+++ b/app/Services/Social/PinterestPublisher.php@@ -34,7 +34,7 @@ class PinterestPublisher$account = $postPlatform->socialAccount;if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -400,34 +400,13 @@ class PinterestPublisher);}- private function refreshToken(SocialAccount $account): void- {- $response = TokenRefreshClient::for(Platform::Pinterest)->send(fn () => Http::asForm()- ->withBasicAuth(- config('services.pinterest.client_id'),- config('services.pinterest.client_secret')- )- ->post($this->baseUrl.'/oauth/token', [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : now()->addDays(30),- ]);- }-/*** Get user's boards for board selection.*/public function getBoards(SocialAccount $account): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}diff --git a/app/Services/Social/ThreadsAnalytics.php b/app/Services/Social/ThreadsAnalytics.phpindex dc245f63f..262589795 100644--- a/app/Services/Social/ThreadsAnalytics.php+++ b/app/Services/Social/ThreadsAnalytics.php@@ -4,14 +4,12 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Carbon\CarbonInterface;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class ThreadsAnalytics@@ -49,7 +47,7 @@ class ThreadsAnalytics}if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -81,7 +79,7 @@ class ThreadsAnalyticsprivate function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -138,19 +136,4 @@ class ThreadsAnalytics{return $this->socialHttp();}-- private function refreshToken(SocialAccount $account): void- {- $response = TokenRefreshClient::for(Platform::Threads)->send(fn () => Http::get(config('trypost.platforms.threads.auth_api').'/refresh_access_token', [- 'grant_type' => 'th_refresh_token',- 'access_token' => $account->access_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.phpindex 135f9dd4b..28d3d19ab 100644--- a/app/Services/Social/ThreadsPublisher.php+++ b/app/Services/Social/ThreadsPublisher.php@@ -4,13 +4,10 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ThreadsPublishException;use App\Models\PostPlatform;-use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Illuminate\Http\Client\Response;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class ThreadsPublisher@@ -33,7 +30,7 @@ class ThreadsPublisher$account = $postPlatform->socialAccount;if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -303,25 +300,6 @@ class ThreadsPublisherthrow new \Exception('Threads media processing timeout after '.$maxAttempts.' attempts');}- private function refreshToken(SocialAccount $account): void- {- // Threads uses long-lived tokens that can be refreshed- $response = TokenRefreshClient::for(Platform::Threads)->send(fn () => Http::get(config('trypost.platforms.threads.auth_api').'/refresh_access_token', [- 'grant_type' => 'th_refresh_token',- 'access_token' => $account->access_token,- ]));-- $data = $response->json();-- $newToken = data_get($data, 'access_token');-- $account->update([- 'access_token' => $newToken,- 'refresh_token' => $newToken,- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }-private function handleApiError(Response $response): never{throw ThreadsPublishException::fromApiResponse($response);diff --git a/app/Services/Social/TikTokAnalytics.php b/app/Services/Social/TikTokAnalytics.phpindex 7532ae3cd..a1a80d634 100644--- a/app/Services/Social/TikTokAnalytics.php+++ b/app/Services/Social/TikTokAnalytics.php@@ -4,13 +4,10 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class TikTokAnalytics@@ -39,7 +36,7 @@ class TikTokAnalyticsprivate function fetchMetricsFromApi(SocialAccount $account): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -160,27 +157,4 @@ class TikTokAnalytics{return $this->socialHttp()->asJson()->withToken($this->accessToken);}-- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for TikTok account');- }-- $response = TokenRefreshClient::for(Platform::TikTok)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.tiktok.api').'/oauth/token/', [- 'client_key' => config('services.tiktok.client_id'),- 'client_secret' => config('services.tiktok.client_secret'),- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/TikTokCreatorInfo.php b/app/Services/Social/TikTokCreatorInfo.phpindex d3fe8ab5a..418316bd0 100644--- a/app/Services/Social/TikTokCreatorInfo.php+++ b/app/Services/Social/TikTokCreatorInfo.php@@ -4,13 +4,10 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class TikTokCreatorInfo@@ -62,7 +59,7 @@ class TikTokCreatorInfoprivate function fetchFresh(SocialAccount $account): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -124,27 +121,4 @@ class TikTokCreatorInfo{return $this->socialHttp()->asJson()->withToken($this->accessToken);}-- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for TikTok account');- }-- $response = TokenRefreshClient::for(Platform::TikTok)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.tiktok.api').'/oauth/token/', [- 'client_key' => config('services.tiktok.client_id'),- 'client_secret' => config('services.tiktok.client_secret'),- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.phpindex c0b875d0e..554e3402e 100644--- a/app/Services/Social/TikTokPublisher.php+++ b/app/Services/Social/TikTokPublisher.php@@ -7,13 +7,11 @@ namespace App\Services\Social;use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\TikTokPublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Illuminate\Http\Client\PendingRequest;use Illuminate\Http\Client\Response;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class TikTokPublisher@@ -38,7 +36,7 @@ class TikTokPublisher$account = $postPlatform->socialAccount;if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -317,30 +315,6 @@ class TikTokPublisherreturn null;}- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for TikTok account');- }-- $response = TokenRefreshClient::for(Platform::TikTok)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.tiktok.api').'/oauth/token/', [- 'client_key' => config('services.tiktok.client_id'),- 'client_secret' => config('services.tiktok.client_secret'),- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);-- }-private function handleApiError(Response $response): never{throw TikTokPublishException::fromApiResponse($response);diff --git a/app/Services/Social/TokenRefreshClient.php b/app/Services/Social/TokenRefreshClient.phpindex 05b1c774c..861389f7b 100644--- a/app/Services/Social/TokenRefreshClient.php+++ b/app/Services/Social/TokenRefreshClient.php@@ -49,7 +49,7 @@ class TokenRefreshClientthrow new PlatformUnavailableException("{$name} API unreachable: {$e->getMessage()}");}- if ($response->serverError()) {+ if ($response->serverError() || $response->status() === 429) {throw new PlatformUnavailableException("{$name} API returned {$response->status()} during token refresh",$response->status(),diff --git a/app/Services/Social/XAnalytics.php b/app/Services/Social/XAnalytics.phpindex 4d620260a..1b22c7e98 100644--- a/app/Services/Social/XAnalytics.php+++ b/app/Services/Social/XAnalytics.php@@ -4,8 +4,6 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;@@ -49,7 +47,7 @@ class XAnalyticsprivate function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -165,7 +163,7 @@ class XAnalytics}if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -200,27 +198,4 @@ class XAnalytics{return $this->socialHttp()->withToken($this->accessToken);}-- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for X account');- }-- $response = TokenRefreshClient::for(Platform::X)->send(fn () => $this->socialHttp()- ->withBasicAuth(config('services.x.client_id'), config('services.x.client_secret'))- ->asForm()- ->post(config('trypost.platforms.x.api').'/oauth2/token', [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.phpindex 56f1a10a8..fefdda530 100644--- a/app/Services/Social/XPublisher.php+++ b/app/Services/Social/XPublisher.php@@ -6,9 +6,7 @@ namespace App\Services\Social;use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\XPublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;-use App\Models\SocialAccount;use App\Services\Media\MediaOptimizer;use App\Services\Social\Concerns\HasSocialHttpClient;use Illuminate\Http\Client\PendingRequest;@@ -39,7 +37,7 @@ class XPublisher// Refresh token if expired or expiring soonif ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -329,28 +327,6 @@ class XPublisherreturn false;}- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for X account');- }-- $response = TokenRefreshClient::for(Platform::X)->send(fn () => Http::asForm()- ->withBasicAuth(config('services.x.client_id'), config('services.x.client_secret'))- ->post("{$this->baseUrl}/oauth2/token", [- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', 7200)),- ]);- }-private function handleApiError(Response $response): never{throw XPublishException::fromApiResponse($response);diff --git a/app/Services/Social/YouTubeAnalytics.php b/app/Services/Social/YouTubeAnalytics.phpindex f082667be..43d012481 100644--- a/app/Services/Social/YouTubeAnalytics.php+++ b/app/Services/Social/YouTubeAnalytics.php@@ -4,15 +4,12 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;use Carbon\CarbonInterface;use Illuminate\Http\Client\PendingRequest;use Illuminate\Support\Facades\Cache;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;class YouTubeAnalytics@@ -50,7 +47,7 @@ class YouTubeAnalytics}if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -102,7 +99,7 @@ class YouTubeAnalyticsprivate function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array{if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -162,27 +159,4 @@ class YouTubeAnalytics{return $this->socialHttp()->withToken($this->accessToken);}-- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for YouTube account');- }-- $response = TokenRefreshClient::for(Platform::YouTube)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.youtube.oauth_api').'/token', [- 'client_id' => config('services.google.client_id'),- 'client_secret' => config('services.google.client_secret'),- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);- }}diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.phpindex 3840766d7..b3a68fd6b 100644--- a/app/Services/Social/YouTubePublisher.php+++ b/app/Services/Social/YouTubePublisher.php@@ -4,10 +4,8 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\YouTubePublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;@@ -36,7 +34,7 @@ class YouTubePublisher$account = $postPlatform->socialAccount;if ($account->is_token_expired || $account->is_token_expiring_soon) {- $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));+ app(ConnectionVerifier::class)->refreshToken($account);$account->refresh();}@@ -224,30 +222,6 @@ class YouTubePublisherreturn $title.$shortsTag;}- private function refreshToken(SocialAccount $account): void- {- if (! $account->refresh_token) {- throw new TokenExpiredException('No refresh token available for YouTube account');- }-- $response = TokenRefreshClient::for(Platform::YouTube)->send(fn () => Http::asForm()- ->post(config('trypost.platforms.youtube.oauth_api').'/token', [- 'client_id' => config('services.google.client_id'),- 'client_secret' => config('services.google.client_secret'),- 'grant_type' => 'refresh_token',- 'refresh_token' => $account->refresh_token,- ]));-- $data = $response->json();-- $account->update([- 'access_token' => data_get($data, 'access_token'),- 'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => data_get($data, 'expires_in') ? now()->addSeconds(data_get($data, 'expires_in')) : null,- ]);-- }-private function handleGoogleError(Exception $e): never{throw YouTubePublishException::fromGoogleException($e);