除了omniauth-google-oauth2的电子邮件,我没有收到任何其他内容

时间:2013-10-16 23:51:56

标签: ruby-on-rails devise omniauth

我在使用omniauth-google-oauth2 gem的应用程序中使用devise plus ominauth,但我的auth哈希只返回电子邮件,即使这是我的范围:

config.omniauth :google_oauth2, GOOGLE_ID, GOOGLE_SECRET,
{
  :scope => "userinfo.email, userinfo.profile, plus.me",
  :prompt => "select_account consent",
  :image_aspect_ratio => "square",
  :image_size => 50
}

这是在我的config\initializers\devise.rb文件中。

我可以使用Google登录,将其显示为我的应用所需的权限,但我在回调中获得的只是电子邮件。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试这个应该适合你............

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"],
    {
      :name => "google",
      :scope => "userinfo.email, userinfo.profile, plus.me, http://gdata.youtube.com",
      :prompt => "select_account",
      :image_aspect_ratio => "square",
      :image_size => 50
    }
end

对于设计,您还应确保拥有OmniAuth回调控制器设置

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.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 @user, :event => :authentication
      else
        session["devise.google_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
  end
end

并绑定或创建用户

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(name: data["name"],
             email: data["email"],
             password: Devise.friendly_token[0,20]
            )
    end
    user
end