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): standardize refresh-token rejection across all publishers

Paulo Castellanod5d3eae839f3
diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.php
index 52eb44d57..7c7ed7965 100644
--- a/app/Services/Social/LinkedInPagePublisher.php
+++ b/app/Services/Social/LinkedInPagePublisher.php
@@ -466,7 +466,10 @@ class LinkedInPagePublisher
]);
if ($response->failed()) {
- $this->handleApiError($response);
+ throw new TokenExpiredException(
+ message: data_get($response->json(), 'error_description', 'Failed to refresh LinkedIn Page token'),
+ platformErrorCode: (string) $response->status(),
+ );
}
$data = $response->json();
diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.php
index 6e55aa49f..37bdd435d 100644
--- a/app/Services/Social/LinkedInPublisher.php
+++ b/app/Services/Social/LinkedInPublisher.php
@@ -447,7 +447,10 @@ class LinkedInPublisher
]);
if ($response->failed()) {
- $this->handleApiError($response);
+ throw new TokenExpiredException(
+ message: data_get($response->json(), 'error_description', 'Failed to refresh LinkedIn token'),
+ platformErrorCode: (string) $response->status(),
+ );
}
$data = $response->json();
diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.php
index f04359b18..ed6c4364e 100644
--- a/app/Services/Social/PinterestPublisher.php
+++ b/app/Services/Social/PinterestPublisher.php
@@ -8,6 +8,7 @@ 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;
@@ -413,8 +414,10 @@ class PinterestPublisher
]);
if ($response->failed()) {
- Log::error('Pinterest token refresh failed', ['body' => $this->redactResponseBody($response->body())]);
- $this->handleApiError($response);
+ 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/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.php
index d169d135a..d799fa68b 100644
--- a/app/Services/Social/ThreadsPublisher.php
+++ b/app/Services/Social/ThreadsPublisher.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Services\Social;
use App\Exceptions\Social\ThreadsPublishException;
+use App\Exceptions\TokenExpiredException;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use App\Services\Social\Concerns\HasSocialHttpClient;
@@ -311,8 +312,10 @@ class ThreadsPublisher
]);
if ($response->failed()) {
- Log::error('Threads token refresh failed', ['body' => $this->redactResponseBody($response->body())]);
- $this->handleApiError($response);
+ 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/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.php
index 5b2faf746..78e4d2d6e 100644
--- a/app/Services/Social/TikTokPublisher.php
+++ b/app/Services/Social/TikTokPublisher.php
@@ -330,8 +330,10 @@ class TikTokPublisher
]);
if ($response->failed()) {
- Log::error('TikTok token refresh failed', ['body' => $this->redactResponseBody($response->body())]);
- $this->handleApiError($response);
+ throw new TokenExpiredException(
+ message: data_get($response->json(), 'error.message', 'Failed to refresh TikTok token'),
+ platformErrorCode: (string) $response->status(),
+ );
}
$data = $response->json();
diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.php
index a9d218220..15aa6ec9d 100644
--- a/app/Services/Social/XPublisher.php
+++ b/app/Services/Social/XPublisher.php
@@ -340,11 +340,6 @@ class XPublisher
]);
if ($response->failed()) {
- Log::error('X token refresh failed', [
- 'status' => $response->status(),
- 'body' => $this->redactResponseBody($response->body()),
- ]);
-
throw new TokenExpiredException(
message: data_get($response->json(), 'error_description', 'Failed to refresh X token'),
platformErrorCode: (string) $response->status(),
diff --git a/tests/Unit/Services/Social/LinkedInPagePublisherTest.php b/tests/Unit/Services/Social/LinkedInPagePublisherTest.php
index 6be43e048..fde565edb 100644
--- a/tests/Unit/Services/Social/LinkedInPagePublisherTest.php
+++ b/tests/Unit/Services/Social/LinkedInPagePublisherTest.php
@@ -167,6 +167,20 @@ test('linkedin page publisher throws exception when no refresh token available',
->toThrow(TokenExpiredException::class, 'No refresh token available for LinkedIn Page account');
});
+test('linkedin page publisher throws TokenExpiredException when refresh_token is rejected', function () {
+ $this->socialAccount->update(['token_expires_at' => now()->subHour()]);
+
+ Http::fake([
+ 'https://www.linkedin.com/oauth/v2/accessToken' => Http::response([
+ 'error' => 'invalid_grant',
+ 'error_description' => 'The refresh token is invalid',
+ ], 400),
+ ]);
+
+ expect(fn () => $this->publisher->publish($this->postPlatform))
+ ->toThrow(TokenExpiredException::class, 'The refresh token is invalid');
+});
+
test('linkedin page publisher handles empty content', function () {
$this->post->update(['content' => '']);
diff --git a/tests/Unit/Services/Social/LinkedInPublisherTest.php b/tests/Unit/Services/Social/LinkedInPublisherTest.php
index 1e68b0147..a647045c0 100644
--- a/tests/Unit/Services/Social/LinkedInPublisherTest.php
+++ b/tests/Unit/Services/Social/LinkedInPublisherTest.php
@@ -141,6 +141,20 @@ test('linkedin publisher throws exception when no refresh token available', func
->toThrow(TokenExpiredException::class, 'No refresh token available for LinkedIn account');
});
+test('linkedin publisher throws TokenExpiredException when refresh_token is rejected', function () {
+ $this->socialAccount->update(['token_expires_at' => now()->subHour()]);
+
+ Http::fake([
+ 'https://www.linkedin.com/oauth/v2/accessToken' => Http::response([
+ 'error' => 'invalid_grant',
+ 'error_description' => 'The refresh token is invalid',
+ ], 400),
+ ]);
+
+ expect(fn () => $this->publisher->publish($this->postPlatform))
+ ->toThrow(TokenExpiredException::class, 'The refresh token is invalid');
+});
+
test('linkedin publisher handles empty content', function () {
$this->post->update(['content' => '']);
diff --git a/tests/Unit/Services/Social/PinterestPublisherTest.php b/tests/Unit/Services/Social/PinterestPublisherTest.php
index 4f1bf9755..fc8090445 100644
--- a/tests/Unit/Services/Social/PinterestPublisherTest.php
+++ b/tests/Unit/Services/Social/PinterestPublisherTest.php
@@ -307,6 +307,32 @@ test('pinterest publisher refreshes token when expired', function () {
expect($this->socialAccount->access_token)->toBe('new-access-token');
});
+test('pinterest publisher throws TokenExpiredException when refresh_token is rejected', function () {
+ $this->socialAccount->update(['token_expires_at' => now()->subHour()]);
+
+ $this->post->update([
+ 'media' => [
+ [
+ 'id' => 'test-media-id',
+ 'path' => 'media/2026-01/image.jpg',
+ 'url' => 'https://example.com/media/2026-01/image.jpg',
+ 'mime_type' => 'image/jpeg',
+ 'original_filename' => 'image.jpg',
+ ],
+ ],
+ ]);
+
+ Http::fake([
+ '*/v5/oauth/token' => Http::response([
+ 'error' => 'invalid_request',
+ 'error_description' => 'Refresh token expired',
+ ], 400),
+ ]);
+
+ expect(fn () => $this->publisher->publish($this->postPlatform))
+ ->toThrow(TokenExpiredException::class, 'Refresh token expired');
+});
+
test('pinterest publisher includes title and link when provided', function () {
$this->postPlatform->update([
'meta' => [
diff --git a/tests/Unit/Services/Social/ThreadsPublisherTest.php b/tests/Unit/Services/Social/ThreadsPublisherTest.php
index cf169017d..ee0b90162 100644
--- a/tests/Unit/Services/Social/ThreadsPublisherTest.php
+++ b/tests/Unit/Services/Social/ThreadsPublisherTest.php
@@ -237,6 +237,19 @@ test('threads publisher refreshes token when expired', function () {
expect($this->socialAccount->access_token)->toBe('new-long-lived-token');
});
+test('threads publisher throws TokenExpiredException when refresh_token is rejected', function () {
+ $this->socialAccount->update(['token_expires_at' => now()->subHour()]);
+
+ Http::fake([
+ 'https://graph.threads.net/refresh_access_token*' => Http::response([
+ 'error' => ['message' => 'Token is invalid', 'code' => 190],
+ ], 400),
+ ]);
+
+ expect(fn () => $this->publisher->publish($this->postPlatform))
+ ->toThrow(TokenExpiredException::class, 'Token is invalid');
+});
+
test('threads publisher waits for media processing', function () {
$this->post->update([
'media' => [
diff --git a/tests/Unit/Services/Social/TikTokPublisherTest.php b/tests/Unit/Services/Social/TikTokPublisherTest.php
index 42be71a1c..a727ebed4 100644
--- a/tests/Unit/Services/Social/TikTokPublisherTest.php
+++ b/tests/Unit/Services/Social/TikTokPublisherTest.php
@@ -237,6 +237,31 @@ test('tiktok publisher throws exception when no refresh token available', functi
->toThrow(TokenExpiredException::class, 'No refresh token available for TikTok account');
});
+test('tiktok publisher throws TokenExpiredException when refresh_token is rejected', function () {
+ $this->socialAccount->update(['token_expires_at' => now()->subHour()]);
+
+ $this->post->update([
+ 'media' => [
+ [
+ 'id' => 'test-media-video',
+ 'path' => 'media/2026-01/test-video.mp4',
+ 'url' => 'https://example.com/media/2026-01/test-video.mp4',
+ 'mime_type' => 'video/mp4',
+ 'original_filename' => 'test-video.mp4',
+ ],
+ ],
+ ]);
+
+ Http::fake([
+ '*/oauth/token/' => Http::response([
+ 'error' => ['code' => 'invalid_grant', 'message' => 'Refresh token expired'],
+ ], 400),
+ ]);
+
+ expect(fn () => $this->publisher->publish($this->postPlatform))
+ ->toThrow(TokenExpiredException::class, 'Refresh token expired');
+});
+
test('tiktok publisher throws exception for unsupported media type', function () {
$this->post->update([
'media' => [