I’m writing a small Python script to fetch an OAuth2 access token using the Client Credentials flow for an internal integration tool. The goal is to automate some API calls without interactive login.
Here’s the snippet I’m using:
import requests
url = "https://api.mypurecloud.com/oauth/token"
payload = {
"grant_type": "client_credentials",
"client_id": "my-client-id",
"client_secret": "my-secret"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
response = requests.post(url, data=payload, headers=headers)
print(response.status_code)
print(response.text)
It keeps returning a 401 Unauthorized. The response body is just {"error":"invalid_client"}. I’ve double-checked the client ID and secret in the Developer Console. They look correct. I’ve also tried encoding them in the Authorization header as Basic auth, but same result.
Is there something specific about the token endpoint that I’m missing? Maybe a specific header requirement or scope that needs to be defined in the client registration? The docs are a bit vague on the exact payload format for this specific flow in Python.