chulkilee / ex_force

A Salesforce REST API wrapper for Elixir
https://hex.pm/packages/ex_force
MIT License
38 stars 27 forks source link

`Jason.DecodeError` from `ExForce.OAuth.get_token/2` #70

Closed ggiill closed 1 year ago

ggiill commented 1 year ago

This just started happening today:

{:error,
 {Tesla.Middleware.JSON, :decode,
  %Jason.DecodeError{
    data: <<...REDACTED...>>,
    position: 0,
    token: nil
  }}}

Dug in a bit, and the order of the Tesla.Middleware makes a difference here for some reason.

These don't work:

ExForce.OAuth.get_token(
  instance_url,
  grant_type: "refresh_token",
  client_id: client_id,
  client_secret: client_secret,
  refresh_token: refresh_token
)
instance_url
|> ExForce.Client.build_oauth_client()
|> Tesla.post(
  instance_url <> "/services/oauth2/token",
  %{
    grant_type: "refresh_token",
    client_id: client_id,
    client_secret: client_secret,
    refresh_token: refresh_token
  }
)
[
  {Tesla.Middleware.BaseUrl, instance_url},
  {Tesla.Middleware.Compression, format: "gzip"},
  Tesla.Middleware.FormUrlencoded,
  {Tesla.Middleware.DecodeJson, engine: Jason},
  {Tesla.Middleware.Headers, []}
]
|> Tesla.client()
|> Tesla.post(
  "/services/oauth2/token",
  %{
    grant_type: "refresh_token",
    client_id: client_id,
    client_secret: client_secret,
    refresh_token: refresh_token
  }
)

(This one ^^^ is basically ExForce.Client.Tesla.build_oauth_client/2)

But moving Tesla.Middleware.DecodeJson first in the list of middleware does, and yields an {:ok, %Tesla.Env{}} tuple with decoded JSON body:

[
  {Tesla.Middleware.DecodeJson, engine: Jason},
  {Tesla.Middleware.BaseUrl, instance_url},
  {Tesla.Middleware.Compression, format: "gzip"},
  Tesla.Middleware.FormUrlencoded,
  {Tesla.Middleware.Headers, []}
]
|> Tesla.client()
|> Tesla.post(
  "/services/oauth2/token",
  %{
  grant_type: "refresh_token",
  client_id: client_id,
  client_secret: client_secret,
  refresh_token: refresh_token
  }
)
ggiill commented 1 year ago

Update: This seems to actually be an issue with bumping tesla from 1.4.4 to 1.5.1...

...though this could be fixed by switching the order of the middleware: https://github.com/chulkilee/ex_force/pull/42