ueberauth / oauth2

An Elixir OAuth 2.0 Client Library
MIT License
749 stars 139 forks source link

Issue with refresh_token #143

Closed sircon closed 8 months ago

sircon commented 5 years ago

I'm having issues with refreshing a Spotify token using the OAuth2 library.

This is the debug I'm getting.

[debug]   OAuth2 Provider Request
  url: "https://accounts.spotify.com/api/token"
  method: :post
  headers: [{"accept", "application/x-www-form-urlencoded"}, {"content-type", "application/x-www-form-urlencoded"}, {"authorization", "Basic OT...DA="}]
  body: "grant_type=refresh_token&refresh_token=AQ...1Lg"
  req_opts: []

Doing the same request using Curl or Postman works alright, but via OAuth2 always gives a 406...

This would be the Curl equivalent:

curl -X "POST" "https://accounts.spotify.com/api/token" \
     -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
     -u '9...1:1...0' \
     --data-urlencode "grant_type=refresh_token" \
     --data-urlencode "refresh_token=AQ...A1Lg"

I wonder what am I doing wrong.

This is my last attempt following the Readme:

refresh_client = OAuth2.Client.new([
  strategy: OAuth2.Strategy.Refresh,
  client_id: System.get_env("VF_SPOTIFY_CLIENT_ID"),
  client_secret: System.get_env("VF_SPOTIFY_CLIENT_SECRET"),
  token_url: "https://accounts.spotify.com/api/token",
  params: %{"refresh_token" => refresh_token}
])

refresh_client = OAuth2.Client.get_token!(refresh_client)

I'm using OAuth2 version 2.0.0, in case this is relevant.

Thanks in advance for the help, Miguel

ku1ik commented 5 years ago

I had similar problem (406 error), and solved it by overriding accept header when constructing client:

OAuth2.Client.new([
  strategy: ...,
  ...,
  headers: [{"accept", "application/json,application/x-www-form-urlencoded"}]
])
thatcherhubbard commented 5 years ago

This bit me too, but I don't see anywhere in the repo where it explicitly sets that value on the Accept header, just Content-Type. Should the OAuth2.Client.new function have a default that sets Accept to */*?

sircon commented 5 years ago

Thanks @sickill, I will give it a try. If it works then it seems that is missing the accepts application/json header as it has the other one.

github-actions[bot] commented 9 months ago

This issue has been automatically marked as "stale:discard". If this issue still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment.

github-actions[bot] commented 8 months ago

Closing this issue after a prolonged period of inactivity. If this issue is still relevant, feel free to re-open the issue. Thank you!