Authlib OAuth2Session: Is it possible to `fetch_token` with a JSON POST body instead of URL params?

Viewed 23

I have an Authlib OAuth2Session and I am trying to authenticate with username and password. Here's what I am doing:

self.session = OAuth2Session(
    token_endpont=f"{API_URL}/oauth/token",
)

self.session.fetch_token(
    url=f"{API_URL}/oauth/token",
    username=username,
    password=password,
    grant_type="password",
)

After looking at the debug logs, it seems that the fetch_token method is passing the username and password as URL parameters. This is how it looks from that method:

send: b'grant_type=password&username=<email>&password=<password>&client_id=None&client_secret='

versus a manual requests call formatted properly which works:

send: b'{"password": "<PW>", "username": "<EMAIL>", "grant_type": "password"}'

Is there a way to make the fetch_token endpoint use a request body formatted with JSON? I have tried adding headers={"Content-Type": "application/json"} as well as passing the username and password into the fetch_token body parameter and neither of those worked.

0 Answers
Related