Python SDK token refresh loop causing 401 errors in automated scripts

I’ve been working on a script to automate some routing configuration backups using the Genesys Cloud Python SDK. The goal is to have a long-running process that fetches user data periodically without manual intervention. I read somewhere that the SDK should handle OAuth token refresh automatically, but I keep hitting 401 Unauthorized errors after about an hour of execution. It’s really frustrating because the documentation implies this is smooth, but my logs are telling a different story.

Here is the basic structure I’m using to initialize the client:

from genesyscloud.auth.api_client import ApiClient
from genesyscloud.auth.configuration import Configuration

config = Configuration()
config.host = 'https://api.mypurecloud.com'
# I'm using a private key file here
config.private_key_file = 'my_key.pem'
config.private_key_password = 'password'
config.client_id = 'my_client_id'
config.client_secret = 'my_client_secret'

api_client = ApiClient(configuration=config)
users_api = api_client.get_client('users')

After this setup, I run a loop that calls users_api.get_users() every 60 seconds. For the first hour or so, it works perfectly fine. Then suddenly, the script crashes with a 401 error. I assumed the SDK would detect the expired access token and use the refresh token to get a new one behind the scenes. It seems like it’s not doing that, or maybe I’m missing a configuration flag to enable automatic refresh.

I tried adding config.refresh_token = '...' but I don’t even have a refresh token since I’m using private key authentication. I’m based in London (Europe/London) and running this on a local Ubuntu server, so time zone drift isn’t an issue. Is there a specific method I need to call to force a refresh, or should I just be catching the 401 error and re-initializing the ApiClient object from scratch every time? I’d prefer not to hardcode a manual refresh loop if the SDK is supposed to handle it. Any pointers on how to make this persistent without the 401 interruptions would be great.