Python requests getting 401 on OAuth2 client credentials grant

Trying to automate some account setup tasks using the Genesys Cloud Platform API. I have a service account set up with the necessary scopes. The goal is to grab an access token using the client credentials flow before hitting the REST endpoints.

I’ve tried the standard POST to the token endpoint. Here is the I’m working with:

import requests

url = "https://api.mypurecloud.com/oauth/token"
payload = {
 "grant_type": "client_credentials",
 "scope": "admin:account:read"
}
headers = {
 "Authorization": "Basic <base64_encoded_client_id_and_secret>"
}

response = requests.post(url, data=payload, headers=headers)
print(response.status_code)
print(response.json())

The response is always a 401 Unauthorized. The error message says “Bad credentials”. I’ve double checked the client ID and secret. They are correct. I’ve also tried encoding them in the header manually just in case. Same result. The service account exists and is active.

What am I missing here? Is the basic auth header format specific? Or is there something wrong with the payload structure?