Python SDK access token expires mid-batch causing 401 on adherence pull

We’ve got a Python script running in our Central time zone office to pull WFM adherence data every 15 minutes. I’m using the genesyscloud SDK because it seemed easier than managing raw HTTP requests. The script kicks off a batch job that iterates through a list of users to get their specific adherence metrics. It works fine for the first few users, but then it crashes halfway through the loop.

The error is a standard 401 Unauthorized. It seems like the access token I got at the start of the script is expiring before the batch finishes. The SDK docs mention automatic token refresh, but maybe I’m missing a step or the config is wrong.

Here’s the setup:

  • Python 3.9
  • genesyscloud SDK version 8.2.1
  • Running locally on a Windows machine
  • OAuth Client Credentials flow

The code looks something like this:

from genesyscloud import authentication, wem
from genesyscloud.rest import ApiException

# Initialize the API client
api_client = authentication.get_client(
 client_id='my_client_id',
 client_secret='my_client_secret',
 org_id='my_org_id'
)

wem_api = wem.WemApi(api_client)

user_ids = ['user1', 'user2', 'user3', 'user4', 'user5'] # In reality this is a list of 50+ users

for user_id in user_ids:
 try:
 # This call hangs for a bit then fails on the later iterations
 response = wem_api.get_wem_adherence_user(user_id=user_id, 
 interval_start='2023-10-01T00:00:00Z',
 interval_end='2023-10-01T01:00:00Z')
 print(f"Got data for {user_id}")
 except ApiException as e:
 if e.status == 401:
 print("Token expired or invalid")
 break
 else:
 print(f"Error: {e}")

I thought the SDK would handle the refresh_token logic in the background. Is there a specific way I need to configure the api_client to force a refresh, or do I need to manually implement a retry loop that fetches a new token?

Also, I noticed the error happens right around the 50-minute mark, which aligns with the default access token expiry. Any ideas on how to keep the session alive for a long-running batch?