Skip to content

Add support for JSON payload in OAuth2 Client Credentials flow#92

Open
dabla wants to merge 1 commit intoColin-b:developfrom
dabla:feature/allow-json-post
Open

Add support for JSON payload in OAuth2 Client Credentials flow#92
dabla wants to merge 1 commit intoColin-b:developfrom
dabla:feature/allow-json-post

Conversation

@dabla
Copy link
Copy Markdown

@dabla dabla commented Apr 24, 2026

This PR extends OAuth2ClientCredentials to support sending the token request payload as JSON instead of the default application/x-www-form-urlencoded.

🚀 Motivation

While RFC 6749 specifies form-encoded payloads for the client credentials grant, some APIs in the wild require the token request to be sent as JSON. This currently makes requests_auth incompatible with those providers out of the box.

In our case, we encountered this limitation when integrating with certain REST APIs from an Apache Airflow environment (via HttpOperator), where JSON-based token endpoints are required.

🧩 What’s included
Introduced a new optional parameter: post_as_json (default: False)

When enabled:
The token request is sent using json=... instead of data=...
client_id and client_secret are included in the JSON body (instead of only via basic auth)
Backward compatible: existing behaviour remains unchanged unless explicitly enabled

🛠️ Example usage

auth = OAuth2ClientCredentials(
    token_url="https://example.com/oauth/token",
    client_id="my-client-id",
    client_secret="my-client-secret",
    post_as_json=True,
)

📌 Notes

This change is particularly useful for non-standard OAuth2 implementations that deviate from the spec.
Unit tests for this feature will be added in a follow-up commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant