Signing a Request with OAuther only works occasionally

I’ve been trying to write a middleware for Tesla, but I’m getting very inconsistent results with my requests. Over half of them result in an OauthException from the server with the message “incorrect oauth_signature”.

The code below is how I’ve written this middleware. Am I using oauther wrong or could this be a bug?

defmodule MyApp.Middleware.OAuth do
  @behaviour Tesla.Middleware

  def call(env, next, _options) do
    |> sign_request()

  defp sign_request(%Tesla.Env{} = env) do
    creds = credentials()
    params = OAuther.sign(to_string(env.method), env.url, string_query(env.query), creds)

    {header, req_params} = OAuther.header(params)

    |> Tesla.put_headers([header])
    |> Map.put(:query, req_params)

  defp credentials(), do: Application.get_env(:my_app, __MODULE__) |> OAuther.credentials()

  defp string_query(query), do:, fn {k, v} -> {to_string(k), v} end)