1

I am using devise plus ominauth in an application using the omniauth-google-oauth2 gem but my auth hash is only returning with email even though this is my scope:

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
}

This is in my config\initializers\devise.rb file.

I can log in in with Google it shows these as the permissions my app needs but all I get in the callback is email.

Any ideas?

4

1 回答 1

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
于 2013-10-16T23:55:38.563 回答