oauth2 icon indicating copy to clipboard operation
oauth2 copied to clipboard

No Case clause matching when setting token_method

Open Epse opened this issue 4 years ago • 0 comments

I need to communicate with a weird OAuth2 server that uses Client Credentials, but only accepts GET to retrieve a token, not POST.

I found the token_method parameter in the documentation / code and set this to :get, but this gives the following error:

** (CaseClauseError) no case clause matching: %{"grant_type" => "client_credentials"}

With hackney traces enabled, this seems to be the sent request (redacted for obious reasons)

   Content: [{module,hackney},
         {line,313},
         {method,get},
         {url,{hackney_url,hackney_ssl,https,<<"URL">>,
                           <<"/api/oauth?grant_type=client_credentials">>,
                           <<"/api/oauth">>,
                           <<"grant_type=client_credentials">>,<<>>,
                           "URL",443,<<>>,<<>>}},
         {headers,[{<<"accept">>,<<"application/json">>},
                   {<<"authorization">>,
                    <<"Basic BASE64STRINGHERE">>}]},
         {body,#{<<"grant_type">> => <<"client_credentials">>}},
         {options,[]}]

Seems to me like this shouldn't have a Body, being a GET request. Code is shown below, when removing the token_method: :get line, the API returns an error that only POST is accepted.

Thanks in advance

Code
defmodule ExMiepGalleryTest do
  use ExUnit.Case
  doctest ExMiepGallery

  test "gets client credential" do
    client =
      OAuth2.Client.new(
        strategy: OAuth2.Strategy.ClientCredentials,
        client_id: "REDACTED",
        client_secret: "REDACTED",
        site: "https://URL/api",
        token_url: "https://URL/api/oauth",
        token_method: :get
      )

    client = OAuth2.Client.get_token!(client)
  end
end

Epse avatar Feb 19 '21 15:02 Epse