Running into a headache with a background worker that processes a large list of users to update their routing profiles via the Genesys Cloud API. The script uses the Node.js Platform SDK to handle authentication, but it’s failing partway through the batch when the access token expires.
I’ve implemented a refresh hook in the SDK initialization to automatically grab a new token when the old one is stale. Here’s the config:
const client = PlatformClient.init({
clientId: process.env.CXONE_CLIENT_ID,
clientSecret: process.env.CXONE_CLIENT_SECRET,
baseUrl: 'https://api.mypurecloud.com',
onRefresh: (refreshToken) => {
console.log('Refreshing token...');
return client.auth.getOAuthClient().refreshToken(refreshToken);
}
});
The first 50 requests go through fine. Then I hit a 401 Unauthorized error on a random request in the middle of the loop. The error payload looks like this:
{
"status": 401,
"code": "unauthorized",
"message": "Access token expired",
"errors": ["The access token is invalid or expired."]
}
I checked the logs and the onRefresh callback doesn’t seem to fire at the right moment, or maybe the SDK isn’t retrying the failed request with the new token. I’m using async/await to process users one by one, so there’s no parallel request storm.
Is there a specific way to ensure the SDK retries the failed API call after a token refresh? Or am I handling the onRefresh hook incorrectly for long-running batch jobs?