Skip to content

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.

fix(social): publish flow honors PlatformUnavailable too

Paulo Castellano32e1f89adb43
diff --git a/app/Jobs/PublishToSocialPlatform.php b/app/Jobs/PublishToSocialPlatform.php
index 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.php
index ec18db806..68c5fc153 100644
--- a/app/Services/Social/BlueskyPublisher.php
+++ b/app/Services/Social/BlueskyPublisher.php
@@ -271,12 +271,12 @@ class BlueskyPublisher
public 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 credentials
if (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.php
index 3b98fa44d..126a4626a 100644
--- a/app/Services/Social/InstagramAnalytics.php
+++ b/app/Services/Social/InstagramAnalytics.php
@@ -211,15 +211,10 @@ class InstagramAnalytics
throw 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.php
index 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 InstagramPublisher
return;
}
- $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.php
index 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 LinkedInPageAnalytics
throw 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.php
index d4a3c6910..7486f5d35 100644
--- a/app/Services/Social/LinkedInPagePublisher.php
+++ b/app/Services/Social/LinkedInPagePublisher.php
@@ -458,19 +458,13 @@ class LinkedInPagePublisher
throw 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.php
index 540e37aea..ed6d230c6 100644
--- a/app/Services/Social/LinkedInPublisher.php
+++ b/app/Services/Social/LinkedInPublisher.php
@@ -439,19 +439,13 @@ class LinkedInPublisher
throw 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.php
index 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 PinterestAnalytics
throw 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.php
index 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 PinterestPublisher
private 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.php
index 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 ThreadsAnalytics
private 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.php
index 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 ThreadsPublisher
private 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.php
index 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 TikTokAnalytics
throw 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.php
index 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 TikTokCreatorInfo
throw 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.php
index 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 TikTokPublisher
throw 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.php
index e1959897c..05b1c774c 100644
--- a/app/Services/Social/TokenRefreshClient.php
+++ b/app/Services/Social/TokenRefreshClient.php
@@ -60,7 +60,13 @@ class TokenRefreshClient
Log::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.php
index 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 XAnalytics
throw 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.php
index ba9ef292e..56f1a10a8 100644
--- a/app/Services/Social/XPublisher.php
+++ b/app/Services/Social/XPublisher.php
@@ -335,19 +335,12 @@ class XPublisher
throw 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.php
index 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 YouTubeAnalytics
throw 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.php
index 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 YouTubePublisher
throw 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.php
index 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();