Repository navigation
fix(tiktok): refresh expired access tokens on demand and share one refresh lock - #1756
Conversation
…fresh lock TikTok access tokens live 24 hours, and nothing refreshed them on demand. Every TikTok failure was a TiktokAPIException, so the SDK's refresh-and-retry (which only answers AuthException) never ran. The commenter identity lookup also called the API directly with the stored token, so once it expired, comment automation was withheld and no other call refreshed it (#1754). - Throw TiktokAuthException (an AuthException) when TikTok rejects the access token (Business API code 40105 or its wording, Open API 401), and keep TikTok's error code on every Business API rejection. - Add a `listComments` action and use it via runAction from the worker's commenter lookup and the builder's missed-comments scan. - Share one refreshTiktokAuth across the SDK, the scheduled job and both builder refresh actions, and run all of them under the SDK auth store's lock. From 2026-12-01 TikTok rotates refresh tokens, so two refreshes racing on one would leave a revoked token behind. - Cache Comment-to-Message by writing only auth.metadata.commentToMessage, so it can no longer write back tokens read before a refresh. Closes #1754 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Hi @rancaHoratiu Fix is in #1756. Every TikTok Business API call now throws a TiktokAuthException on error code 40105 or on the "access token is incorrect / expired" wording. The commenter lookup now goes through runAction, so it refreshes the token and retries once. Could you test it before we merge? We haven't confirmed code 40105 against a real expired token yet, and your stuck connection is the ideal case: Run the fix/tiktok-token-refresh-recovery branch (builder and worker, including the schedule worker). |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Tested on our production install (Coolify, EEA account) with the Setup: I put back a genuinely expired access token (expired Nov 14) into Result: case 1, it recovered on its own.
Raw TikTok error for the expired token (I called {"code":40105,"message":"Access token is incorrect or has been revoked."}So 40105 is the right code to map to the auth exception. One small suggestion: logging the refresh at INFO level ("token rejected, refreshed") would make this easier to spot in production. Right now it's silent. Thanks for the quick fix! |
|
Thanks a lot for testing this on a real install, and for the raw TikTok response. That confirms Good point about the logging. #1797 adds an info-level log line when a rejected token gets refreshed and the call is retried: It includes the integration name and the provider's rejection reason, but no token values. It's in the shared SDK refresh path, so every OAuth channel with a refresh handler (TikTok, Instagram, Messenger, LINE…) logs it, not just TikTok. Nothing changes when a refresh fails: it still errors, and a terminal failure still marks the connection offline. |
Summary
Fixes #1754. A TikTok access token lives 24 hours. Once it expired, nothing refreshed it, and comment automation stopped without any error.
Why the token was never refreshed
AuthException. TikTok threwTiktokAPIExceptionfor everything, so this never ran on TikTok."has been revoked"toAUTH_FAILED_PATTERNS, as the issue suggests, would not fix this. Those patterns only label an error; they never turn it into anAuthException. The same list also decides whether a connection counts as revoked, so an ordinary expired token would push people to reconnect.Changes
TiktokAuthException(anAuthException), thrown when TikTok rejects the access token. It is recognized by Business API code40105or its wording, or an Open API 401. Every Business API rejection now keeps TikTok's own error code.listCommentsintegration action. The worker's commenter lookup and the builder's missed-comments scan call it throughrunAction, so the SDK refreshes an expiring token first and retries once on a rejection.Preparing for refresh-token rotation (TikTok, effective 2026-12-01)
From that date, each refresh returns a new refresh token and invalidates the old one, so a refresh that runs with the old token, or writes it back, leaves the connection dead.
refreshTiktokAuthis now shared by the SDKrefreshAuth, the scheduled job and both builder refresh actions. It always saves the returnedrefresh_token.tiktokIntegrationService.withTokenRefreshLock, and read the auth inside it. Before this, the job and the builder buttons locked onauth:refresh:tiktok:<id>, while the SDK locked onauth:refresh:connection:<id>.cacheCommentToMessageStatusnow writes onlyauth.metadata.commentToMessage(withjsonb_set). It used to write back the wholeauthit had read before calling TikTok, which could restore tokens replaced by a refresh in the meantime.Out of scope
openIdis unique, so one install cannot connect the same account twice. Two separate installs that share one TikTok account (for example staging and production) will still invalidate each other's tokens after 2026-12-01. Code cannot prevent that, so it belongs in the release notes.Test plan
undefined.TiktokAuthException; other rejections stayTiktokAPIExceptionwith their code.integrations/tiktok(151 tests),packages/business,apps/workerandapps/builderall pass.pnpm lint, type checks for integration-tiktok, business, worker and builder;invariant-guardclean.40105is taken from TikTok's docs and has not been checked against a real expired token yet (the wording match covers it either way). Check it on staging: moveexpiresAtinto the past, then comment on a video.🤖 Generated with Claude Code