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.

Keep Instagram/Threads tokens extended while still valid

Paulo Castellano1bb67b7abb3d
diff --git a/app/Enums/SocialAccount/Platform.php b/app/Enums/SocialAccount/Platform.php
index e66e21645..3ca8f5a77 100644
--- a/app/Enums/SocialAccount/Platform.php
+++ b/app/Enums/SocialAccount/Platform.php
@@ -261,6 +261,22 @@ enum Platform: string
};
}
+ /**
+ * Whether this platform refreshes by extending the access_token itself
+ * (Instagram/Threads long-lived tokens) instead of exchanging a separate
+ * refresh_token. Extension-model tokens cannot be refreshed once expired,
+ * so they must be refreshed proactively while still valid — the opposite
+ * of rotating refresh_token platforms, which we avoid refreshing until
+ * they actually expire so we don't rotate a still-valid single-use token.
+ */
+ public function extendsAccessTokenOnRefresh(): bool
+ {
+ return match ($this) {
+ self::Instagram, self::Threads => true,
+ default => false,
+ };
+ }
+
public function queue(): string
{
return 'social-'.$this->value;
diff --git a/app/Jobs/RefreshSocialToken.php b/app/Jobs/RefreshSocialToken.php
index 53df7da8c..c3678c96a 100644
--- a/app/Jobs/RefreshSocialToken.php
+++ b/app/Jobs/RefreshSocialToken.php
@@ -24,7 +24,14 @@ class RefreshSocialToken implements ShouldQueue
public function handle(ConnectionVerifier $verifier): void
{
try {
- $verifier->verify($this->account);
+ if ($this->account->platform->extendsAccessTokenOnRefresh()) {
+ // Instagram/Threads extend the long-lived token itself and
+ // can't be refreshed once expired, so extend it while it's
+ // still valid instead of waiting for it to fail.
+ $verifier->refreshToken($this->account);
+ } else {
+ $verifier->verify($this->account);
+ }
} catch (PlatformUnavailableException $e) {
Log::warning('Token refresh skipped: platform unavailable', [
'account_id' => $this->account->id,
diff --git a/app/Models/SocialAccount.php b/app/Models/SocialAccount.php
index 173314598..21a3f5730 100644
--- a/app/Models/SocialAccount.php
+++ b/app/Models/SocialAccount.php
@@ -86,6 +86,26 @@ class SocialAccount extends Model
);
}
+ protected function isTokenExpiringSoon(): Attribute
+ {
+ return Attribute::make(
+ get: fn () => $this->token_expires_at && $this->token_expires_at->isBefore(now()->addMinutes(15)),
+ );
+ }
+
+ /**
+ * Whether the token should be refreshed before use. Rotating-refresh-token
+ * platforms are only refreshed once actually expired, to avoid rotating a
+ * still-valid single-use refresh_token; extension-model platforms
+ * (Instagram/Threads) must be refreshed while still valid because their
+ * token can't be extended once expired.
+ */
+ public function needsProactiveTokenRefresh(): bool
+ {
+ return $this->is_token_expired
+ || ($this->platform->extendsAccessTokenOnRefresh() && $this->is_token_expiring_soon);
+ }
+
protected function avatarUrl(): Attribute
{
return Attribute::make(
diff --git a/app/Services/Social/AbstractLinkedInPublisher.php b/app/Services/Social/AbstractLinkedInPublisher.php
index 527f98be0..febc78588 100644
--- a/app/Services/Social/AbstractLinkedInPublisher.php
+++ b/app/Services/Social/AbstractLinkedInPublisher.php
@@ -66,7 +66,7 @@ abstract class AbstractLinkedInPublisher
$this->account = $postPlatform->socialAccount;
$this->hasRetried = false;
- if ($this->account->is_token_expired) {
+ if ($this->account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($this->account);
}
diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.php
index 01db59387..15d4850da 100644
--- a/app/Services/Social/BlueskyPublisher.php
+++ b/app/Services/Social/BlueskyPublisher.php
@@ -48,7 +48,7 @@ class BlueskyPublisher
$service = $account->meta['service'] ?? config('trypost.platforms.bluesky.default_service');
// Refresh token if needed
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/InstagramAnalytics.php b/app/Services/Social/InstagramAnalytics.php
index 87dd98645..42356e1e3 100644
--- a/app/Services/Social/InstagramAnalytics.php
+++ b/app/Services/Social/InstagramAnalytics.php
@@ -44,7 +44,7 @@ class InstagramAnalytics
$this->baseUrl = $account->platform->instagramGraphBaseUrl();
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -87,7 +87,7 @@ class InstagramAnalytics
{
$this->baseUrl = $account->platform->instagramGraphBaseUrl();
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.php
index ca6eb0f49..176bdcc3b 100644
--- a/app/Services/Social/InstagramPublisher.php
+++ b/app/Services/Social/InstagramPublisher.php
@@ -28,7 +28,7 @@ class InstagramPublisher
$account = $postPlatform->socialAccount;
$this->baseUrl = $account->platform->instagramGraphBaseUrl();
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/LinkedInPageAnalytics.php b/app/Services/Social/LinkedInPageAnalytics.php
index e20651f7a..5573a7497 100644
--- a/app/Services/Social/LinkedInPageAnalytics.php
+++ b/app/Services/Social/LinkedInPageAnalytics.php
@@ -50,7 +50,7 @@ class LinkedInPageAnalytics
return ['unsupported' => true, 'reason' => 'missing_post_id'];
}
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -79,7 +79,7 @@ class LinkedInPageAnalytics
private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/PinterestAnalytics.php b/app/Services/Social/PinterestAnalytics.php
index 6bb04561b..7b3bc6ce6 100644
--- a/app/Services/Social/PinterestAnalytics.php
+++ b/app/Services/Social/PinterestAnalytics.php
@@ -46,7 +46,7 @@ class PinterestAnalytics
return ['unsupported' => true, 'reason' => 'missing_post_id'];
}
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -90,7 +90,7 @@ class PinterestAnalytics
private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.php
index c17114f32..b56f0b970 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) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -407,7 +407,7 @@ class PinterestPublisher
*/
public function getBoards(SocialAccount $account): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/ThreadsAnalytics.php b/app/Services/Social/ThreadsAnalytics.php
index 10a2b2a5d..67c572f68 100644
--- a/app/Services/Social/ThreadsAnalytics.php
+++ b/app/Services/Social/ThreadsAnalytics.php
@@ -46,7 +46,7 @@ class ThreadsAnalytics
return ['unsupported' => true, 'reason' => 'missing_post_id'];
}
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -77,7 +77,7 @@ class ThreadsAnalytics
private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.php
index 36c089167..d3740bd15 100644
--- a/app/Services/Social/ThreadsPublisher.php
+++ b/app/Services/Social/ThreadsPublisher.php
@@ -30,7 +30,7 @@ class ThreadsPublisher
$account = $postPlatform->socialAccount;
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/TikTokAnalytics.php b/app/Services/Social/TikTokAnalytics.php
index 3ca14bbfc..7cc7d5e0f 100644
--- a/app/Services/Social/TikTokAnalytics.php
+++ b/app/Services/Social/TikTokAnalytics.php
@@ -35,7 +35,7 @@ class TikTokAnalytics
private function fetchMetricsFromApi(SocialAccount $account): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/TikTokCreatorInfo.php b/app/Services/Social/TikTokCreatorInfo.php
index 1a26ef0c2..ce0586749 100644
--- a/app/Services/Social/TikTokCreatorInfo.php
+++ b/app/Services/Social/TikTokCreatorInfo.php
@@ -58,7 +58,7 @@ class TikTokCreatorInfo
*/
private function fetchFresh(SocialAccount $account): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.php
index 14c8a0680..0c50fa17e 100644
--- a/app/Services/Social/TikTokPublisher.php
+++ b/app/Services/Social/TikTokPublisher.php
@@ -43,7 +43,7 @@ class TikTokPublisher
$account = $postPlatform->socialAccount;
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/XAnalytics.php b/app/Services/Social/XAnalytics.php
index 052ffd804..618a87eff 100644
--- a/app/Services/Social/XAnalytics.php
+++ b/app/Services/Social/XAnalytics.php
@@ -46,7 +46,7 @@ class XAnalytics
private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -161,7 +161,7 @@ class XAnalytics
return ['unsupported' => true, 'reason' => 'missing_post_id'];
}
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.php
index 5c7cbec76..6b82f1a7e 100644
--- a/app/Services/Social/XPublisher.php
+++ b/app/Services/Social/XPublisher.php
@@ -37,8 +37,7 @@ class XPublisher
$account = $postPlatform->socialAccount;
- // Refresh only when the token is actually expired
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/YouTubeAnalytics.php b/app/Services/Social/YouTubeAnalytics.php
index c5dd3e69e..f43b16dbd 100644
--- a/app/Services/Social/YouTubeAnalytics.php
+++ b/app/Services/Social/YouTubeAnalytics.php
@@ -46,7 +46,7 @@ class YouTubeAnalytics
return ['unsupported' => true, 'reason' => 'missing_post_id'];
}
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
@@ -97,7 +97,7 @@ class YouTubeAnalytics
private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array
{
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.php
index 667e613b7..f3f6e4438 100644
--- a/app/Services/Social/YouTubePublisher.php
+++ b/app/Services/Social/YouTubePublisher.php
@@ -33,7 +33,7 @@ class YouTubePublisher
$account = $postPlatform->socialAccount;
- if ($account->is_token_expired) {
+ if ($account->needsProactiveTokenRefresh()) {
app(ConnectionVerifier::class)->refreshToken($account);
}
diff --git a/tests/Feature/Jobs/RefreshSocialTokenTest.php b/tests/Feature/Jobs/RefreshSocialTokenTest.php
index 6299504ed..e9e443992 100644
--- a/tests/Feature/Jobs/RefreshSocialTokenTest.php
+++ b/tests/Feature/Jobs/RefreshSocialTokenTest.php
@@ -2,6 +2,7 @@
declare(strict_types=1);
+use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Exceptions\PlatformUnavailableException;
use App\Exceptions\TokenExpiredException;
@@ -60,6 +61,30 @@ test('proactive refresh does NOT rotate the X refresh token while the access tok
expect($this->account->fresh()->status)->toBe(Status::Connected);
});
+test('proactive refresh EXTENDS a still-valid Instagram token (extension-model platform)', function () {
+ $account = SocialAccount::factory()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'platform' => Platform::Instagram,
+ 'status' => Status::Connected,
+ 'access_token' => 'old-ig-token',
+ 'token_expires_at' => now()->addMinutes(20),
+ ]);
+
+ Http::fake([
+ config('trypost.platforms.instagram.auth_api').'/refresh_access_token*' => Http::response([
+ 'access_token' => 'extended-ig-token',
+ 'expires_in' => 5184000,
+ ], 200),
+ ]);
+
+ (new RefreshSocialToken($account))->handle(app(ConnectionVerifier::class));
+
+ // Instagram/Threads extend the token itself and can't refresh once expired,
+ // so a still-valid token IS extended proactively — unlike rotating platforms.
+ Http::assertSent(fn ($request) => str_contains($request->url(), 'refresh_access_token'));
+ expect($account->fresh()->access_token)->toBe('extended-ig-token');
+});
+
test('refresh job marks account as TokenExpired when refresh_token is rejected', function () {
Queue::fake();
diff --git a/tests/Feature/SocialAccountModelTest.php b/tests/Feature/SocialAccountModelTest.php
index d28bbf70f..9be609c75 100644
--- a/tests/Feature/SocialAccountModelTest.php
+++ b/tests/Feature/SocialAccountModelTest.php
@@ -3,6 +3,7 @@
declare(strict_types=1);
use App\Enums\Notification\Type;
+use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Events\NotificationCreated;
use App\Jobs\SendNotification;
@@ -20,6 +21,33 @@ beforeEach(function () {
$this->workspace = Workspace::factory()->create(['user_id' => $this->owner->id]);
});
+// ---- needsProactiveTokenRefresh ----
+
+test('needsProactiveTokenRefresh: rotating platforms refresh only when expired, extension platforms while still valid', function () {
+ // Rotating (X): a still-valid token that is merely expiring soon is NOT refreshed.
+ $xValid = SocialAccount::factory()->x()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'token_expires_at' => now()->addMinutes(10),
+ ]);
+ expect($xValid->needsProactiveTokenRefresh())->toBeFalse();
+
+ // Rotating (X): once actually expired, it is refreshed.
+ $xExpired = SocialAccount::factory()->x()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'token_expires_at' => now()->subMinute(),
+ ]);
+ expect($xExpired->needsProactiveTokenRefresh())->toBeTrue();
+
+ // Extension-model (Instagram): a still-valid token that is expiring soon MUST
+ // be refreshed while valid — its token can't be extended once expired.
+ $igValid = SocialAccount::factory()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'platform' => Platform::Instagram,
+ 'token_expires_at' => now()->addMinutes(10),
+ ]);
+ expect($igValid->needsProactiveTokenRefresh())->toBeTrue();
+});
+
// ---- markAsTokenExpired ----
test('markAsTokenExpired updates status and dispatches notification when transitioning from connected', function () {