Trying to automate a daily export of interaction analytics from CXone into an S3 bucket. The idea is simple: grab a client_credentials token, loop through the last 24 hours of data in 15-minute chunks, and write the JSON blobs to S3.
Here’s the flow in the script:
def fetch_and_upload():
token = get_oauth_token()
client = genesyscloud.platformclient_v2.Configuration()
client.access_token = token
analytics_api = genesyscloud.platformclient_v2.AnalyticsApi(client)
start_time = datetime.utcnow() - timedelta(hours=24)
end_time = datetime.utcnow()
s3_client = boto3.client('s3')
while start_time < end_time:
query = {
'dateFrom': start_time.isoformat(),
'dateTo': (start_time + timedelta(minutes=15)).isoformat(),
'groupBy': ['user.id'],
'metrics': ['wrapup.time']
}
try:
resp = analytics_api.post_analytics_interactions_get(query)
payload = json.dumps(resp.to_dict())
s3_client.put_object(
Bucket='my-analytics-bucket',
Key=f'data/{start_time.strftime("%Y%m%d%H%M")}.json',
Body=payload
)
except Exception as e:
print(f'Failed chunk: {start_time} - {e}')
break
start_time += timedelta(minutes=15)
The first few chunks upload fine. Then, around the 40th iteration, the API call throws a 401 Unauthorized error. The token is valid for an hour, and the whole job shouldn’t take more than 10 minutes. The error isn’t consistent though. Sometimes it fails at chunk 30, sometimes chunk 50.
Is the Python SDK not handling the token refresh correctly in this loop? Or is there a rate limit I’m hitting that’s causing the auth check to fail? I’ve tried catching the 401 and re-fetching the token inside the loop, but that feels like a hack. There has to be a cleaner way to handle long-running fetch jobs with the SDK.