We’re migrating a legacy Python script that manually calls the OAuth token endpoint to use the official Genesys Cloud Platform SDK. The script processes about 20k contacts in a single run, updating attributes via api.update_user. After the first batch, we start getting 401 Unauthorized errors even though the SDK should handle token refresh automatically. Is there a config setting we’re missing to force a refresh before the long-running loop? Here’s the init code:
config = Configuration()
client = PlatformClient(config)
The SDK handles the refresh under the hood, but you’re probably hitting a race condition where two threads try to refresh the same token at once, or the refresh fails silently because your client ID/secret permissions are stale. Don’t try to force a refresh manually; that breaks the internal lock.
Instead, wrap your update_user call in a retry loop with exponential backoff. If you get a 401, wait a bit and try again. The SDK will refresh the token on the next attempt.
import time
from purecloud_platform_client.rest import ApiException
def safe_update(client, user_id, body, max_retries=3):
for attempt in range(max_retries):
try:
return client.user_api.update_user(user_id, body=body)
except ApiException as e:
if e.status == 401 and attempt < max_retries - 1:
time.sleep(2 ** attempt) # Backoff
continue
raise
Also check your scope. If you’re using a client credential grant, make sure user:write is actually granted to that app in the developer portal. Sometimes the token is valid but lacks the specific scope for bulk writes.