CXone REST Proxy 401 mid-execution in Studio script

Running a long batch process in that updates contact custom attributes. The script loops through a CSV file and makes PUT requests to the CXone API using the REST proxy action. It works fine for the first 50-100 records. Then it starts failing with a 401 Unauthorized error. The token seems to expire while the script is still running.

I’m initializing the token at the start of the script:

ASSIGN token = GetOAuthToken(clientId, clientSecret)

Then in the loop:

REST proxy
 url: https://api.nice-incontact.com/ccapi/v2/contacts/{id}
 method: PUT
 header: Authorization: Bearer {token}
 body: {jsonPayload}

The error response is just {"error": "invalid_token"}. I can’t just refresh the token inside the loop because the GetOAuthToken action isn’t designed to be called repeatedly in a tight loop, and it’s slow. Is there a way to check if the token is expired and refresh it only when needed? Or is there a better pattern for handling long-running scripts in that need valid auth throughout?

You’re probably hitting the token expiry mid-loop. Try moving the GetOAuthToken call inside your loop or adding a simple check for expiry before each batch of requests. It’s a bit of extra overhead, but it stops the 401s cold.