Hey everyone,
I’m trying to figure out the right way to handle API authentication for some Python scripts that pull WFM adherence data. We recently switched our org to SAML SSO for human users, and now my old OAuth client credentials flow is breaking.
The scripts run on a server, so there’s no user interaction to trigger the SAML login redirect. I tried creating a new OAuth token with the analytics:read scope, but when I use it to hit /api/v2/analytics/agents/realtime/intervals, I get a 401 Unauthorized error. The response body just says "error_description": "Invalid or expired token".
Here is the basic auth request I’m sending:
import requests
url = "https://{{my_env}}.mypurecloud.com/oauth/token"
auth = requests.auth.HTTPBasicAuth('my_client_id', 'my_client_secret')
data = {'grant_type': 'client_credentials', 'scope': 'analytics:read'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post(url, auth=auth, data=data, headers=headers)
print(r.json())
The token generation itself works fine and returns an access_token. It’s only when I use that token for the actual API calls that it fails. I read somewhere that SAML SSO might disable standard OAuth for certain scopes, but that seems odd for programmatic access.
Do I need to set up a specific user account that bypasses SSO? Or is there a different grant type I should be using? I’ve been stuck on this for a couple of days. Any pointers would be great.