Running a Python script to push 5k DFO messages via the CXone API. The logic grabs an access token at the start and loops through the payload list, hitting /api/v2/digital/messaging/messages for each chunk. Everything works fine for the first 2k or so. Then the script hits a random 401 Unauthorized error and dies. I’m not using the official SDK for this part, just requests with the bearer token in the header. The token has a standard 30-minute TTL. It seems like the batch processing takes longer than that, or maybe there’s a race condition I’m missing. Here’s the request setup:
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, json=chunk)
Should I be checking the expires_in claim manually and refreshing the token within the loop before it hits zero? Or is there a retry mechanism I should be wiring up instead of just failing hard?