【发布时间】:2015-06-15 02:09:47
【问题描述】:
现在卡了几天。设计,Omniauth,Spotify。我正在尝试允许用户使用 spotify 登录/注册。它说我的重定向 URI 无效,但我已将其设置为 'http://localhost:3000/users/auth/spotify/callback/' 在 Spotify 网站上。
这是错误...由于某些原因,它正在执行 2 个omniauth 请求,这可能是因为我有 gem 'omniauth' 和 gem 'omniauth-oauth2',我将在下面显示。
Started GET "/users/auth/spotify" for 127.0.0.1 at 2015-04-09 10:20:31 -0500
Started GET "/users/auth/spotify" for 127.0.0.1 at 2015-04-09 10:20:31 -0500
I, [2015-04-09T10:20:31.062564 #63684] INFO -- omniauth: (spotify) Request phase initiated.
Started GET "/users/auth/spotify" for 127.0.0.1 at 2015-04-09 10:20:34 -0500
Started GET "/users/auth/spotify" for 127.0.0.1 at 2015-04-09 10:20:34 -0500
I, [2015-04-09T10:20:34.782951 #63684] INFO -- omniauth: (spotify) Request phase initiated.
Started GET "/users/auth/spotify/callback?code=STUFF&state=MORESTUFF" for 127.0.0.1 at 2015-04-09 10:20:36 -0500
Started GET "/users/auth/spotify/callback?code=MOREMORESTUFF" for 127.0.0.1 at 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 by Devise::OmniauthCallbacksController#failure as HTML
Processing by Devise::OmniauthCallbacksController#failure as HTML
Parameters: {"code"=>"HAHAGOTYA", "state"=>"NOSEEUM"}
Parameters: {"code"=>"BYEBYE", "state"=>"BYEBYEBYE"}
Redirected to http://localhost:3000/users/sign_in
Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 6ms
Completed 302 Found in 6ms
宝石文件
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'
路线
devise_for :users, :controller => {:omniauth_callbacks => "omniauth_callbacks"}
root 'users#index'
用户.rb
class User
include Mongoid::Document
#include Mongoid::Paperclip
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :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 do |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
现在,与我阅读的内容相反,我同时拥有 config/initializers/omniauth.rb 和 config/initializers/devise.rb 配置,用于 spotify 密钥和密钥。我的一部分认为这可能是破坏一切的原因,因为它两次初始化请求。
无论如何这里是 devise.rb
config.omniauth :spotify, ENV['SPOTIFY_ID'], ENV['SPOTIFY_KEY'], provider_ignores_state: true
和omniauth.rb
provider :spotify, ENV["SPOTIFY_ID"], ENV["SPOTIFY_KEY"], :provider_ignores_state => true
{
:scope => "playlist-read-private, playlist-modify-public, playlist-modify-private"
}
任何帮助将不胜感激。如果需要,我可以提供更多信息。
更新
我已删除 :provider_ignore_state,现在正在删除
Started GET "/users/auth/spotify/callback?code=WUPSstate=WUPS" for 127.0.0.1 at 2015-04-09 10:53:55 -0500
Started GET "/users/auth/spotify/callback?code=WUPS&state=WUPS" for 127.0.0.1 at 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
我还更改了 session_store.rb 以包含
domain: localhost:3000
和 application_controller.rb
protect_from_forgery with: :null_session
【问题讨论】:
-
宝石 'omniauth-spotify' 的这种行为给我带来了很多痛苦。改用 gem 'rspotify' 你会没事的:)
-
你发现了吗?我得到了完全相同的错误:(
标签: ruby-on-rails ruby oauth devise omniauth