Executing a background CoroutineScope on Android to paginate conversation history through /api/v2/conversations/messaging/conversations. Processing batches of ~500 records. The OAuthClient instance is wired to refreshAccessToken(), yet the export routine consistently terminates with 401 Unauthorized after approximately 200 successful fetches. The failure pattern indicates a race condition where the accessToken expiry intersects with the refreshAccessToken() invocation, or the underlying WebMessagingSdk auth provider isn’t propagating the rotated credentials to concurrent in-flight OkHttpClient calls quickly enough.
Attempted to isolate the failure by wrapping the pagination loop in a synchronous retry mechanism:
while (response.code == 401) {
val newToken = oauthClient.refreshAccessToken()
apiClient.accessToken = newToken.accessToken
response = apiClient.getConversations()
}
The 401 Unauthorized responses persist intermittently despite the explicit token swap. Debug output consistently prints Token expires in: 0 seconds immediately preceding the coroutine cancellation. While the WebMessagingSdk maintains a stable authenticated session for foreground UI components, this background export worker appears to be operating on a stale auth context. Network interceptor logs confirm that the AuthInterceptor is not intercepting or retrying the 401 responses, indicating a potential disconnect between the SDK’s internal token cache and the custom apiClient instance.