Google OAuth 2.0 + Ruby on Rails 404:“未找到。身份验证通过。”

问题描述 投票:0回答:1

下面是相关代码(如果我缺少任何信息,请告诉我!)。

routes.rb:


devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }

devise_scope :user do 
    match '/sessions/user', to: 'devise/sessions#create', via: :post
    get '/join' => 'users/registrations#new'
end

resources :users do
    resources :video_lessons do
      resources :likes
    end
end

resources :users, only: [:show, :index]

devise.rb:

if Rails.env.production?
    config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID_PROD'[, ENV['GOOGLE_CLIENT_SECRET_PROD'], {}
else
    config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID_DEV'], ENV['GOOGLE_CLIENT_SECRET_DEV'], {}
end

omniauth_callbacks_controller.rb

  def google_oauth2
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env['omniauth.auth'])

    if @user.persisted?
      flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
      sign_in_and_redirect @user, event: :authentication
    else
      session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores
      redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
    end
  end

devise / registrations / new.html.erb

<%= link_to "Sign up with Google", user_google_oauth2_omniauth_authorize_path %>

我的开发人员控制台中的授权重定向URI为:http://localhost:3000/users/auth/google_oauth2https://localhost:3000/users/auth/google_oauth2

我完全遵循文档(https://github.com/zquestz/omniauth-google-oauth2),无济于事。

谢谢您的帮助!

ruby-on-rails ruby google-oauth omniauth google-oauth2
1个回答
1
投票

最近我通过此guide完成了此工作。official wiki example是更具描述性的资源,也是指南的来源。

commit显示了我使用我首先提到的指南在我的rails登录页面上为设置google和github而更改的所有文件。

只需确保在使用任何一种指南时都删除了多余的数据,因为它可能会溢出某些会话存储。指南

包含此重要信息。因此,在omniauth_callbacks_controller.rb的这一行中,您当前的代码是正确的
...
    else
          session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores
....

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.