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
fix(social): publish flow honors PlatformUnavailable too
Paulo Castellano32e1f89adb43
diff --git a/app/Jobs/PublishToSocialPlatform.php b/app/Jobs/PublishToSocialPlatform.phpindex dd0128eab..0ee2b7191 100644--- a/app/Jobs/PublishToSocialPlatform.php+++ b/app/Jobs/PublishToSocialPlatform.php@@ -10,6 +10,7 @@ use App\Enums\PostPlatform\Status as PostPlatformStatus;use App\Enums\SocialAccount\Platform as SocialPlatform;use App\Enums\SocialAccount\Status;use App\Events\PostPlatformStatusUpdated;+use App\Exceptions\PlatformUnavailableException;use App\Exceptions\Social\SocialPublishException;use App\Exceptions\TokenExpiredException;use App\Mail\PostPublished;@@ -110,6 +111,19 @@ class PublishToSocialPlatform implements ShouldQueue$result = $publisher->publish($this->postPlatform);$this->postPlatform->markAsPublished(data_get($result, 'id'), data_get($result, 'url'));break;+ } catch (PlatformUnavailableException $e) {+ Log::warning('Publish skipped: platform unavailable', [+ 'post_platform_id' => $this->postPlatform->id,+ 'platform' => $this->postPlatform->platform->value,+ 'error' => $e->getMessage(),+ ]);++ $this->postPlatform->markAsFailed($e->getMessage(), [+ 'category' => 'platform_unavailable',+ 'http_status' => $e->httpStatus,+ 'failed_at' => now()->toIso8601String(),+ ]);+ break;} catch (TokenExpiredException $e) {if ($attempt < $maxAttempts) {try {diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.phpindex ec18db806..68c5fc153 100644--- a/app/Services/Social/BlueskyPublisher.php+++ b/app/Services/Social/BlueskyPublisher.php@@ -271,12 +271,12 @@ class BlueskyPublisherpublic function refreshToken(SocialAccount $account): void{$service = $account->meta['service'] ?? config('trypost.platforms.bluesky.default_service');+ $client = TokenRefreshClient::for(Platform::Bluesky);- // Try refresh first- $response = $this->socialHttp()->withToken($account->refresh_token)- ->post("{$service}/xrpc/com.atproto.server.refreshSession");+ try {+ $response = $client->send(fn () => $this->socialHttp()->withToken($account->refresh_token)+ ->post("{$service}/xrpc/com.atproto.server.refreshSession"));- if ($response->successful()) {$data = $response->json();$account->update(['access_token' => data_get($data, 'accessJwt'),@@ -285,37 +285,27 @@ class BlueskyPublisher]);return;+ } catch (TokenExpiredException) {+ // refresh token rejected (4xx) — fall back to re-auth below}- Log::warning('Bluesky refresh token failed, trying re-authentication', [- 'status' => $response->status(),- ]);-- // If refresh fails, re-authenticate with stored credentialsif (isset($account->meta['password'])) {try {- $password = decrypt($account->meta['password']);- $identifier = $account->meta['identifier'];-- $response = Http::post("{$service}/xrpc/com.atproto.server.createSession", [- 'identifier' => $identifier,- 'password' => $password,+ $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),]);- if ($response->successful()) {- $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 (\Exception $e) {- Log::error('Bluesky re-authentication failed', [- 'error' => $e->getMessage(),- ]);+ return;+ } catch (TokenExpiredException) {+ // re-auth rejected with stored credentials — fall through}}diff --git a/app/Services/Social/InstagramAnalytics.php b/app/Services/Social/InstagramAnalytics.phpindex 3b98fa44d..126a4626a 100644--- a/app/Services/Social/InstagramAnalytics.php+++ b/app/Services/Social/InstagramAnalytics.php@@ -211,15 +211,10 @@ class InstagramAnalyticsthrow new TokenExpiredException('No refresh token available for Instagram account');}- $response = Http::get(config('trypost.platforms.instagram.auth_api').'/refresh_access_token', [+ $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,- ]);-- if ($response->failed()) {- Log::error('Instagram token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('Instagram token refresh failed');- }+ ]));$data = $response->json();diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.phpindex 85c8b0fbc..9ad27dd5e 100644--- a/app/Services/Social/InstagramPublisher.php+++ b/app/Services/Social/InstagramPublisher.php@@ -8,7 +8,6 @@ use App\Enums\PostPlatform\ContentType;use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\InstagramPublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Media\MediaOptimizer;@@ -403,16 +402,10 @@ class InstagramPublisherreturn;}- $response = Http::get(config('trypost.platforms.instagram.auth_api').'/refresh_access_token', [+ $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,- ]);-- if ($response->failed()) {- Log::error('Instagram token refresh failed', ['body' => $this->redactResponseBody($response->body())]);-- throw new TokenExpiredException('Failed to refresh Instagram token');- }+ ]));$data = $response->json();$newToken = data_get($data, 'access_token');diff --git a/app/Services/Social/LinkedInPageAnalytics.php b/app/Services/Social/LinkedInPageAnalytics.phpindex 1c2c5b6a7..98c1d5197 100644--- a/app/Services/Social/LinkedInPageAnalytics.php+++ b/app/Services/Social/LinkedInPageAnalytics.php@@ -4,6 +4,7 @@ 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;@@ -240,17 +241,13 @@ class LinkedInPageAnalyticsthrow new TokenExpiredException('No refresh token available for LinkedIn Page account');}- $response = 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'),- ]);-- if ($response->failed()) {- Log::error('LinkedIn token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('LinkedIn token refresh failed');- }+ $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();diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.phpindex d4a3c6910..7486f5d35 100644--- a/app/Services/Social/LinkedInPagePublisher.php+++ b/app/Services/Social/LinkedInPagePublisher.php@@ -458,19 +458,13 @@ class LinkedInPagePublisherthrow new TokenExpiredException('No refresh token available for LinkedIn Page account');}- $response = 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'),- ]);-- if ($response->failed()) {- throw new TokenExpiredException(- message: data_get($response->json(), 'error_description', 'Failed to refresh LinkedIn Page token'),- platformErrorCode: (string) $response->status(),- );- }+ $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();diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.phpindex 540e37aea..ed6d230c6 100644--- a/app/Services/Social/LinkedInPublisher.php+++ b/app/Services/Social/LinkedInPublisher.php@@ -439,19 +439,13 @@ class LinkedInPublisherthrow new TokenExpiredException('No refresh token available for LinkedIn account');}- $response = 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'),- ]);-- if ($response->failed()) {- throw new TokenExpiredException(- message: data_get($response->json(), 'error_description', 'Failed to refresh LinkedIn token'),- platformErrorCode: (string) $response->status(),- );- }+ $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();diff --git a/app/Services/Social/PinterestAnalytics.php b/app/Services/Social/PinterestAnalytics.phpindex 78745fb66..2a343b9ea 100644--- a/app/Services/Social/PinterestAnalytics.php+++ b/app/Services/Social/PinterestAnalytics.php@@ -4,6 +4,7 @@ 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;@@ -167,18 +168,13 @@ class PinterestAnalyticsthrow new TokenExpiredException('No refresh token available for Pinterest account');}- $response = Http::withBasicAuth(+ $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,- ]);-- if ($response->failed()) {- Log::error('Pinterest token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('Pinterest token refresh failed');- }+ ]));$data = $response->json();diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.phpindex ed6c4364e..116b93f5e 100644--- a/app/Services/Social/PinterestPublisher.php+++ b/app/Services/Social/PinterestPublisher.php@@ -8,7 +8,6 @@ use App\Enums\PostPlatform\ContentType;use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\PinterestPublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Media\MediaOptimizer;@@ -403,7 +402,7 @@ class PinterestPublisherprivate function refreshToken(SocialAccount $account): void{- $response = Http::asForm()+ $response = TokenRefreshClient::for(Platform::Pinterest)->send(fn () => Http::asForm()->withBasicAuth(config('services.pinterest.client_id'),config('services.pinterest.client_secret')@@ -411,14 +410,7 @@ class PinterestPublisher->post($this->baseUrl.'/oauth/token', ['grant_type' => 'refresh_token','refresh_token' => $account->refresh_token,- ]);-- if ($response->failed()) {- throw new TokenExpiredException(- message: data_get($response->json(), 'error_description', 'Failed to refresh Pinterest token'),- platformErrorCode: (string) $response->status(),- );- }+ ]));$data = $response->json();diff --git a/app/Services/Social/ThreadsAnalytics.php b/app/Services/Social/ThreadsAnalytics.phpindex a34fd8f5a..dc245f63f 100644--- a/app/Services/Social/ThreadsAnalytics.php+++ b/app/Services/Social/ThreadsAnalytics.php@@ -4,7 +4,7 @@ declare(strict_types=1);namespace App\Services\Social;-use App\Exceptions\TokenExpiredException;+use App\Enums\SocialAccount\Platform;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;@@ -141,15 +141,10 @@ class ThreadsAnalyticsprivate function refreshToken(SocialAccount $account): void{- $response = Http::get(config('trypost.platforms.threads.auth_api').'/refresh_access_token', [+ $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,- ]);-- if ($response->failed()) {- Log::error('Threads token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('Threads token refresh failed');- }+ ]));$data = $response->json();diff --git a/app/Services/Social/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.phpindex d799fa68b..135f9dd4b 100644--- a/app/Services/Social/ThreadsPublisher.php+++ b/app/Services/Social/ThreadsPublisher.php@@ -4,8 +4,8 @@ declare(strict_types=1);namespace App\Services\Social;+use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ThreadsPublishException;-use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;use App\Services\Social\Concerns\HasSocialHttpClient;@@ -306,17 +306,10 @@ class ThreadsPublisherprivate function refreshToken(SocialAccount $account): void{// Threads uses long-lived tokens that can be refreshed- $response = Http::get(config('trypost.platforms.threads.auth_api').'/refresh_access_token', [+ $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,- ]);-- if ($response->failed()) {- throw new TokenExpiredException(- message: data_get($response->json(), 'error.message', 'Failed to refresh Threads token'),- platformErrorCode: (string) $response->status(),- );- }+ ]));$data = $response->json();diff --git a/app/Services/Social/TikTokAnalytics.php b/app/Services/Social/TikTokAnalytics.phpindex 7d30333df..7532ae3cd 100644--- a/app/Services/Social/TikTokAnalytics.php+++ b/app/Services/Social/TikTokAnalytics.php@@ -4,6 +4,7 @@ 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;@@ -166,17 +167,13 @@ class TikTokAnalyticsthrow new TokenExpiredException('No refresh token available for TikTok account');}- $response = 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,- ]);-- if ($response->failed()) {- Log::error('TikTok token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('TikTok token refresh failed');- }+ $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();diff --git a/app/Services/Social/TikTokCreatorInfo.php b/app/Services/Social/TikTokCreatorInfo.phpindex 42a4858de..d3fe8ab5a 100644--- a/app/Services/Social/TikTokCreatorInfo.php+++ b/app/Services/Social/TikTokCreatorInfo.php@@ -4,6 +4,7 @@ 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;@@ -130,17 +131,13 @@ class TikTokCreatorInfothrow new TokenExpiredException('No refresh token available for TikTok account');}- $response = 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,- ]);-- if ($response->failed()) {- Log::error('TikTok token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('TikTok token refresh failed');- }+ $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();diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.phpindex 78e4d2d6e..c0b875d0e 100644--- a/app/Services/Social/TikTokPublisher.php+++ b/app/Services/Social/TikTokPublisher.php@@ -4,6 +4,7 @@ declare(strict_types=1);namespace App\Services\Social;+use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\TikTokPublishException;use App\Exceptions\TokenExpiredException;@@ -322,19 +323,13 @@ class TikTokPublisherthrow new TokenExpiredException('No refresh token available for TikTok account');}- $response = 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,- ]);-- if ($response->failed()) {- throw new TokenExpiredException(- message: data_get($response->json(), 'error.message', 'Failed to refresh TikTok token'),- platformErrorCode: (string) $response->status(),- );- }+ $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();diff --git a/app/Services/Social/TokenRefreshClient.php b/app/Services/Social/TokenRefreshClient.phpindex e1959897c..05b1c774c 100644--- a/app/Services/Social/TokenRefreshClient.php+++ b/app/Services/Social/TokenRefreshClient.php@@ -60,7 +60,13 @@ class TokenRefreshClientLog::error("TokenRefreshClient: {$name} token refresh failed", ['body' => $this->redactBody($response->body()),]);- throw new TokenExpiredException("Failed to refresh {$name} token");++ $body = $response->json();+ $message = data_get($body, 'error_description')+ ?? data_get($body, 'error.message')+ ?? "Failed to refresh {$name} token";++ throw new TokenExpiredException($message, platformErrorCode: (string) $response->status());}return $response;diff --git a/app/Services/Social/XAnalytics.php b/app/Services/Social/XAnalytics.phpindex ff8d07ed9..4d620260a 100644--- a/app/Services/Social/XAnalytics.php+++ b/app/Services/Social/XAnalytics.php@@ -4,6 +4,7 @@ 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;@@ -206,18 +207,13 @@ class XAnalyticsthrow new TokenExpiredException('No refresh token available for X account');}- $response = $this->socialHttp()+ $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,- ]);-- if ($response->failed()) {- Log::error('X token refresh failed', ['body' => $this->redactResponseBody($response->body())]);- throw new TokenExpiredException('X token refresh failed');- }+ ]));$data = $response->json();diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.phpindex ba9ef292e..56f1a10a8 100644--- a/app/Services/Social/XPublisher.php+++ b/app/Services/Social/XPublisher.php@@ -335,19 +335,12 @@ class XPublisherthrow new TokenExpiredException('No refresh token available for X account');}- $response = Http::asForm()+ $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,- ]);-- if ($response->failed()) {- throw new TokenExpiredException(- message: data_get($response->json(), 'error_description', 'Failed to refresh X token'),- platformErrorCode: (string) $response->status(),- );- }+ ]));$data = $response->json();diff --git a/app/Services/Social/YouTubeAnalytics.php b/app/Services/Social/YouTubeAnalytics.phpindex 41f6a90c9..f082667be 100644--- a/app/Services/Social/YouTubeAnalytics.php+++ b/app/Services/Social/YouTubeAnalytics.php@@ -4,6 +4,7 @@ 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;@@ -168,18 +169,13 @@ class YouTubeAnalyticsthrow new TokenExpiredException('No refresh token available for YouTube account');}- $response = 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,- ]);-- if ($response->failed()) {- Log::error('YouTube token refresh failed', ['body' => $this->redactResponseBody($response->body())]);-- throw new TokenExpiredException('Failed to refresh YouTube token');- }+ $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();diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.phpindex 1f67c6d0f..3840766d7 100644--- a/app/Services/Social/YouTubePublisher.php+++ b/app/Services/Social/YouTubePublisher.php@@ -4,6 +4,7 @@ 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;@@ -229,18 +230,13 @@ class YouTubePublisherthrow new TokenExpiredException('No refresh token available for YouTube account');}- $response = 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,- ]);-- if ($response->failed()) {- Log::error('YouTube token refresh failed', ['body' => $this->redactResponseBody($response->body())]);-- throw new TokenExpiredException('Failed to refresh YouTube token');- }+ $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();diff --git a/tests/Feature/Jobs/PublishToSocialPlatformTest.php b/tests/Feature/Jobs/PublishToSocialPlatformTest.phpindex 4f39d5ed0..30d4c1758 100644--- a/tests/Feature/Jobs/PublishToSocialPlatformTest.php+++ b/tests/Feature/Jobs/PublishToSocialPlatformTest.php@@ -9,6 +9,7 @@ use App\Enums\UserWorkspace\Role;use App\Events\PostPlatformStatusUpdated;use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\LinkedInPublishException;+use App\Exceptions\PlatformUnavailableException;use App\Exceptions\TokenExpiredException;use App\Jobs\PublishToSocialPlatform;use App\Jobs\SendNotification;@@ -109,6 +110,28 @@ test('publish to social platform marks account as token expired on auth failure'expect($this->socialAccount->status)->toBe(AccountStatus::TokenExpired);});+test('publish to social platform does NOT mark account expired when platform is unavailable', function () {+ Event::fake();+ Mail::fake();++ $publisher = Mockery::mock(LinkedInPublisher::class);+ $publisher->shouldReceive('publish')->andThrow(+ new PlatformUnavailableException('LinkedIn API returned 503 during token refresh', 503)+ );++ $this->app->instance(LinkedInPublisher::class, $publisher);++ (new PublishToSocialPlatform($this->postPlatform))->handle();++ $this->postPlatform->refresh();+ $this->socialAccount->refresh();++ expect($this->postPlatform->status)->toBe(PlatformStatus::Failed);+ expect($this->postPlatform->error_context['category'] ?? null)->toBe('platform_unavailable');+ expect($this->postPlatform->error_context['http_status'] ?? null)->toBe(503);+ expect($this->socialAccount->status)->toBe(AccountStatus::Connected);+});+test('publish to social platform updates post status when all platforms finished', function () {Event::fake();