Skip to content

fix(publisher): refresh session tokens (Bluesky) before publishing#120

Merged
JanSchm merged 1 commit into
brightbeanxyz:mainfrom
tecnomanu:fix/bluesky-session-token-refresh
Jul 2, 2026
Merged

fix(publisher): refresh session tokens (Bluesky) before publishing#120
JanSchm merged 1 commit into
brightbeanxyz:mainfrom
tecnomanu:fix/bluesky-session-token-refresh

Conversation

@tecnomanu

Copy link
Copy Markdown
Contributor

Problem

Scheduled Bluesky posts intermittently fail with:

Bluesky API error 400: {"error":"ExpiredToken","message":"Token has expired"}

The publish engine only refreshes access tokens for OAuth2 providers. The gate in PublishEngine._dispatch_to_provider is:

if account.token_expires_at and account.is_token_expiring_soon and provider.auth_type == AuthType.OAUTH2:
    access_token = account.refresh_oauth_token(provider)

Bluesky uses AT Protocol session auth (AuthType.SESSION), so this branch is never taken for it. Its accessJwt expires after only a few hours, so any scheduled post that waits past the token's TTL publishes with a stale token and gets ExpiredToken. OAuth2 platforms (Meta, Google, LinkedIn) are unaffected because their tokens are long-lived and already covered.

Fix

Gate the refresh on the presence of a stored refresh token instead of the auth type:

if account.token_expires_at and account.is_token_expiring_soon and account.oauth_refresh_token:
    access_token = account.refresh_oauth_token(provider)

This makes session providers that store a refresh token (Bluesky stores the refreshJwt in oauth_refresh_token) renew via com.atproto.server.refreshSession before each publish. The whole mechanism already existed — SocialAccount.refresh_oauth_token()provider.refresh_token()refreshSession, persisting the rotated refreshJwt — only the engine gate was excluding it. BlueskyProvider.create_session already populates token_expires_at from the JWT's exp claim, so is_token_expiring_soon fires correctly.

The refresh stays best-effort (wrapped in try/except): on failure the old token is kept and the publish attempt surfaces the real error, so this is safe for any provider whose refresh_token() may raise. The now-unused AuthType import is dropped.

Testing

Verified end-to-end against a live self-hosted deployment: a Bluesky post that had been failing with ExpiredToken published successfully after this change.

The publish engine only refreshed access tokens for OAuth2 providers,
skipping session-based providers. Bluesky uses AT Protocol session auth:
its accessJwt expires after only a few hours, so scheduled posts failed
with `ExpiredToken: Token has expired` whenever the stored token had aged
past its TTL.

Gate the refresh on the presence of a stored refresh token instead of the
auth type, so Bluesky (and any session provider that stores a refreshJwt)
renews via refreshSession before each publish. Refresh stays best-effort:
on failure we keep the old token and let the publish surface the error.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tecnomanu tecnomanu requested a review from JanSchm as a code owner July 2, 2026 14:37
@tecnomanu

Copy link
Copy Markdown
Contributor Author

I've already tried it and it works well.
imagen

@JanSchm

JanSchm commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

thank you for fixing, I'll merge it in.

@JanSchm JanSchm merged commit df313f2 into brightbeanxyz:main Jul 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants