Home > Blockchain >  Rails 7, OAuth2 unable to set redirect_uri in get_token
Rails 7, OAuth2 unable to set redirect_uri in get_token

Time:06-03

I have brand new Rails 7.0.3, Ruby 3.1.2, app with oauth2 gem (v. 1.4.9).

To initialize OAuth I use:

client = OAuth2::Client.new(client_auth[:client_id], client_auth[:client_secret], site: client_auth[:api_url])

redirect_to client.auth_code.authorize_url(scope: 'v2 read write', redirect_uri: https://my_app.com/auth/')

After success login to target site, user is redirected to my controller with code. I cannot get token using client.auth_code.get_token

client.auth_code.get_token(code, redirect_uri: 'https://my_app.com/auth/')
# when redirect_uri param is different than localhost it raise Exception: 

/gems/faraday-2.3.0/lib/faraday/encoders/nested_params_encoder.rb:131:in 'new_context': no implicit conversion of String into Integer (TypeError)

I cannot use localhost as redirect_uri because provider check this parameter. Any ideas why this doesn't work?

Gemfile.lock
...
oauth2 (1.4.9)
      faraday (>= 0.17.3, < 3.0)
      jwt (>= 1.0, < 3.0)
      multi_json (~> 1.3)
      multi_xml (~> 0.5)
      rack (>= 1.2, < 3)

CodePudding user response:

After many tries I realized that token url is different than standard. When I set client in this way all works fine.

client = OAuth2::Client.new(client_auth[:client_id], client_auth[:client_secret], site: client_auth[:api_url], token_url: 'api/open/oauth/token')
  • Related