Python OAuth2 Client Credentials returning 400 Bad Request

Hey folks,

I’m trying to script a simple token refresh for a backend service using the Genesys Cloud API. I’ve got the client ID and secret from the admin portal, but the requests library keeps throwing a 400 error. The docs say to use client_credentials as the grant type, which I’m doing.

Here’s the snippet:

import requests

url = "https://api.mypurecloud.com/oauth/token"
headers = {
 "Content-Type": "application/x-www-form-urlencoded"
}
payload = {
 "grant_type": "client_credentials",
 "client_id": "my_client_id",
 "client_secret": "my_client_secret"
}

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

The response body just says Invalid grant. I’ve double-checked the credentials and they look fine. Am I missing a scope parameter or something? The standard docs don’t mention scopes for client credentials flow, but maybe I need to specify one?

Also, should I be encoding the client ID/secret differently? I tried base64 encoding them in the Authorization header instead of the body, but that gave a 401 Unauthorized. Seems like the body approach is right based on the Swagger docs, but it’s failing validation.

Anyone else hit this with Python?