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
env
|> sign_request()
|> Tesla.run(next)
end
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)
env
|> Tesla.put_headers([header])
|> Map.put(:query, req_params)
end
defp credentials(), do: Application.get_env(:my_app, __MODULE__) |> OAuther.credentials()
defp string_query(query), do: Enum.map(query, fn {k, v} -> {to_string(k), v} end)
end