Does anyone know the most robust way to intercept and refresh an OAuth access token when it expires during a long-running batch operation in a Playwright TypeScript test suite? I am automating E2E scenarios for our custom Genesys Cloud agent desktop, which relies heavily on the genesys-cloud-webmessaging-sdk and internal REST calls via page.evaluate. My current implementation fetches a token at test setup, but the 3600-second TTL causes intermittent 401 Unauthorized errors when iterating through large datasets of user provisioning requests.
I attempted to wrap the API calls in a retry logic that checks for status === 401 and triggers a /oauth/token refresh, but this breaks the Playwright session state or causes race conditions with the iframe authentication handshake. The error payload returns {"error": "invalid_token", "error_description": "Token has expired"}. I need a method to silently refresh the token and re-inject it into the browser context without triggering a full page reload or losing the page.context().storageState() integrity.