0

我得到了设计设置并为我的应用程序工作。当我添加

gem omniauth-google-oauth2

sign_in_and_redirect我在控制器方法中遇到错误。

错误:

NoMethodError (undefined method `serialize_into_session' for String:Class):
  app/controllers/users/omniauth_callbacks_controller.rb:9:in `google_oauth2'

代码:

omn​​iauth_callbacks_controller.rb

def google_oauth2
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect sites_path, :event => :authentication
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
end

用户.rb

def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
    data = access_token.info
    user = User.where(:email => data["email"]).first

    unless user
      user = User.create(email: data["email"],
                         password: Devise.friendly_token[0, 20]
      )
    end
    user
end

设计.rb

require "omniauth-google-oauth2"
config.omniauth :google_oauth2, 'CLIENT_ID', 'CLIENT_SECRET', {access_type: "offline", approval_prompt: ""}

如果我遗漏了什么,请告诉我。

4

1 回答 1

0

您的配置很好,但我在这种情况下使用omniauth-google-oauth2这个 gem https://rubygems.org/gems/omniauth-openid

添加到您/config/initializer/devise.rb的下一个:

require 'openid/store/filesystem'
config.omniauth :open_id, :store => OpenID::Store::Filesystem.new('/tmp'), :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id', :require => 'omniauth-openid'

在你omniauth_callbacks_controller.rb改变方法User.find_for_google_oauth2...之后User.find_for_open_id...

在您的user模型上更改self.find_for_google_oauth2by self.find_for_open_id

请尝试使用此宝石!

于 2013-01-25T09:16:26.350 回答