gpt4 book ai didi

ruby-on-rails - 设计/谷歌 OAuth 2 : Not found. 认证通路

转载 作者:行者123 更新时间:2023-12-03 17:38:17 25 4
gpt4 key购买 nike

我遵循了 omniauth-google-oauth2 的自述文件中的教程gem 并且当我单击我的根 (@ pages#home) 上的链接时,<%= link_to "Sign up with Google", user_google_oauth2_omniauth_authorize_path %> ,我收到错误:

Not found. Authentication passthru.



我已经确认 ENV 变量在那里。我一直在寻找类似的主题,但没有运气。知道我做错了什么吗?

在 route :
Rails.application.routes.draw do
devise_for :users, controllers: { :omniauth_callbacks => "users/omniauth_callbacks" }

我的 omniauth_callbacks_controller 位于 /controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
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
end

在我的 devise.rb文件:
config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {
name: 'my-project',
scope: 'email',
prompt: 'select_account',
image_aspect_ratio: 'original',
image_size: 100,
ssl_verify: false
}

在我的 User.rb 中:
devise :rememberable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2]

def self.from_omniauth(access_token)
data = access_token.info
user = User.where(:email => data["email"]).first

# Uncomment the section below if you want users to be created if they don't exist
# unless user
# user = User.create(name: data["name"],
# email: data["email"],
# password: Devise.friendly_token[0,20]
# )
# end

user
end

最佳答案

我解决了将以下内容添加到 config/initializers/omniauth.rb 的问题:

OmniAuth.config.allowed_request_methods = %i[get]
解释:
以上是 https://github.com/zquestz/omniauth-google-oauth2#usage中的配置:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end
OmniAuth.config.allowed_request_methods = %i[get]
但没有
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end
因为这已经在您的 config/initializers/devise.rb 中提供了:
  config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {
name: 'my-project',
scope: 'email',
prompt: 'select_account',
image_aspect_ratio: 'original',
image_size: 100,
ssl_verify: false
}

关于ruby-on-rails - 设计/谷歌 OAuth 2 : Not found. 认证通路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42990547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com