简体   繁体   English

使用Google Open-id访问令牌信息设计Omniauth

[英]Devise Omniauth with google open-id access token information

I'm new in rails and I'm trying to integrate omniauth and the login with google. 我是Rails的新手,我正在尝试将omniauth和登录名与Google集成。 Surfing on the web i found a way to do it, but i got this error: 在网上冲浪时,我找到了一种方法,但出现此错误:

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'

I think that is because i don't access correctly the access token information. 我认为这是因为我没有正确访问访问令牌信息。

here is the code: 这是代码:

Model users.rb 模型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 omn​​iauth_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

devise.rb devise.rb

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

I don't know what else i can show you. 我不知道我还能给你看什么。

For me the problem is here: 对我来说,问题在这里:

data = access_token['user_info'] 数据= access_token ['user_info']

access_token['user_info'] returns null. access_token ['user_info']返回null。

Am I doing something wrong? 难道我做错了什么? Is there other way to access the 'user_info' and then the e-mail? 还有其他方法可以访问“ user_info”然后再访问电子邮件?

Thanks in advance. 提前致谢。 Hope i'm clear. 希望我明白。

You should just be using data = access_token['info'] instead of data = access_token['user_info'] 您应该只使用data = access_token['info']而不是data = access_token['user_info']

From there you have access to data['email] , data['first_name'] , and data['last_name'] . 从那里您可以访问data['email]data['first_name']data['last_name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM