Running a nightly batch job to update contact attributes for about 50k records. We’re using a Python script with requests library. The logic is straightforward. Get the initial access token. Loop through chunks of data. Call /api/v2/contacts.
The issue starts around chunk 40. The token expires. I have a try/except block catching 401 errors. When caught, it calls the refresh token endpoint.
Here is the refresh logic:
if response.status_code == 401:
refresh_resp = requests.post(token_url, data=refresh_payload)
new_token = refresh_resp.json()['access_token']
headers['Authorization'] = f'Bearer {new_token}'
response = requests.put(url, json=payload, headers=headers)
The refresh request returns 200 OK. The new token looks valid. But the subsequent PUT request to /api/v2/contacts still fails with 401 Unauthorized. It’s like the platform doesn’t recognize the newly minted token immediately.
We’ve checked the expiration time in the token payload. It’s fine. The client credentials are correct. This happens consistently after about 3 hours of runtime. Is there a cache issue on the API gateway side? Or am I missing a step in the token rotation process for bulk operations?