【问题标题】:Devise Omniauth with google open-id access token information使用 google open-id 访问令牌信息设计 Omniauth
【发布时间】:2023-03-10 11:20:01
【问题描述】:

我是 Rails 的新手,我正在尝试将omniauth 和登录与谷歌集成。 在网上冲浪我找到了一种方法,但我得到了这个错误:

undefined method `[]' for nil:NilClass

app/models/user.rb:22:in `find_for_open_id'
app/controllers/users/omniauth_callbacks_controller.rb:17:in `open_id'

我认为这是因为我没有正确访问访问令牌信息。

代码如下:

模型 users.rb

def self.find_for_open_id(access_token, signed_in_resource=nil)    
    data = access_token['user_info']
    if user = User.find_by_email(data["email"])
    user
    else # Create a user with a stub password.
      User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
    end
  end

omniauth_callbacks_controller.rb

def open_id
    # You need to implement the method below in your model
    @user = User.find_for_open_id(env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.open:id_data"] = env["openid.ext1"]
      redirect_to new_user_registration_url
    end
  end

设计.rb

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

我不知道我还能给你看什么。

对我来说问题就在这里:

data = access_token['user_info']

access_token['user_info'] 返回 null。

我做错了吗?还有其他方法可以访问“user_info”然后访问电子邮件吗?

提前致谢。希望我很清楚。

【问题讨论】:

    标签: ruby-on-rails devise openid omniauth google-openid


    【解决方案1】:

    您应该只使用data = access_token['info'] 而不是data = access_token['user_info']

    从那里您可以访问data['email]data['first_name']data['last_name']

    【讨论】:

    • 谢谢。在家里我会测试它。 ;)
    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多