ruby - Issue with OAUTH in Rails 4 using Spotify -
ruby - Issue with OAUTH in Rails 4 using Spotify -
been stuck couple days now. devise, omniauth, spotify. i'm trying allow users signin/signup spotify. says redirect uri invalid, have set 'http://localhost:3000/users/auth/spotify/callback/' on spotify website.
here's error... reasons doing 2 omniauth requests may because have gem 'omniauth' , gem 'omniauth-oauth2' i'll show below.
started "/users/auth/spotify" 127.0.0.1 @ 2015-04-09 10:20:31 -0500 started "/users/auth/spotify" 127.0.0.1 @ 2015-04-09 10:20:31 -0500 i, [2015-04-09t10:20:31.062564 #63684] info -- omniauth: (spotify) request phase initiated. started "/users/auth/spotify" 127.0.0.1 @ 2015-04-09 10:20:34 -0500 started "/users/auth/spotify" 127.0.0.1 @ 2015-04-09 10:20:34 -0500 i, [2015-04-09t10:20:34.782951 #63684] info -- omniauth: (spotify) request phase initiated. started "/users/auth/spotify/callback?code=stuff&state=morestuff" 127.0.0.1 @ 2015-04-09 10:20:36 -0500 started "/users/auth/spotify/callback?code=moremorestuff" 127.0.0.1 @ 2015-04-09 10:20:36 -0500 i, [2015-04-09t10:20:36.669918 #63684] info -- omniauth: (spotify) callback phase initiated. e, [2015-04-09t10:20:37.468309 #63684] error -- omniauth: (spotify) authentication failure! invalid_credentials: oauth2::error, invalid_grant: invalid redirect uri {"error":"invalid_grant","error_description":"invalid redirect uri"} processing devise::omniauthcallbackscontroller#failure html processing devise::omniauthcallbackscontroller#failure html parameters: {"code"=>"hahagotya", "state"=>"noseeum"} parameters: {"code"=>"byebye", "state"=>"byebyebye"} redirected http://localhost:3000/users/sign_in redirected http://localhost:3000/users/sign_in completed 302 found in 6ms completed 302 found in 6ms
gemfile
ruby '2.2.0' gem 'rails', '4.2.0' gem 'mongoid', '~> 4.0.2' gem 'bson', '~> 2.2' gem 'moped', '~> 2.0.0' gem 'omniauth', '~> 1.2.2' gem 'oauth2' gem 'omniauth-spotify' gem 'devise'
routes
devise_for :users, :controller => {:omniauth_callbacks => "omniauth_callbacks"} root 'users#index'
user.rb
class user include mongoid::document #include mongoid::paperclip # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:spotify, :facebook] field :email, type: string, default: "" field :encrypted_password, type: string, default: "" ## recoverable field :reset_password_token, type: string field :reset_password_sent_at, type: time ## rememberable field :remember_created_at, type: time ## trackable field :sign_in_count, type: integer, default: 0 field :current_sign_in_at, type: time field :last_sign_in_at, type: time field :current_sign_in_ip, type: string field :last_sign_in_ip, type: string field :provider, type: string field :uid, type: string field :name, type: string embeds_many :posts embeds_many :workouts embeds_many :routines embeds_many :meals has_many :followers has_many :followings def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create |user| user.provider = auth.provider user.uid = auth.uid user.email = auth.info.email user.password = devise.friendly_token[0,20] end end end
omniauth_callbacks_controller.rb
class omniauthcallbackscontroller < devise::omniauthcallbackscontroller def spotify @user = user.from_omniauth(request.env['omniauth.auth']) sign_in_and_redirect @user end end
now, contrary i've read, have both config/initializers/omniauth.rb , config/initializers/devise.rb configuration spotify secret , key. part of me thinks may breaking everything, because it's initializing request twice.
anyway here's devise.rb
config.omniauth :spotify, env['spotify_id'], env['spotify_key'], provider_ignores_state: true
and omniauth.rb
provider :spotify, env["spotify_id"], env["spotify_key"], :provider_ignores_state => true { :scope => "playlist-read-private, playlist-modify-public, playlist-modify-private" }
any help appreciated. can provide more info if needed.
updatei've removed :provider_ignore_state, , getting
started "/users/auth/spotify/callback?code=wupsstate=wups" 127.0.0.1 @ 2015-04-09 10:53:55 -0500 started "/users/auth/spotify/callback?code=wups&state=wups" 127.0.0.1 @ 2015-04-09 10:53:55 -0500 i, [2015-04-09t10:53:55.732722 #64694] info -- omniauth: (spotify) callback phase initiated. e, [2015-04-09t10:53:55.733274 #64694] error -- omniauth: (spotify) authentication failure! csrf_detected: omniauth::strategies::oauth2::callbackerror, csrf_detected | csrf detected
i've changed session_store.rb include
domain: localhost:3000
and application_controller.rb
protect_from_forgery with: :null_session
looks issue in onmniauth. see here: https://github.com/intridea/omniauth-oauth2/issues/81
forking gem , changing dependency works. can utilize fork fixes now...
see commit: https://github.com/morgz/rspotify/commit/969d208770e407526c03edea81c3c686e9b79705
https://github.com/morgz/rspotify
ruby-on-rails ruby oauth devise omniauth
Comments
Post a Comment