0

私はレールが初めてで、omniauth とログインを Google に統合しようとしています。Web サーフィンをしていて、それを行う方法を見つけましたが、次のエラーが発生しました。

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

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

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」にアクセスしてから電子メールにアクセスする他の方法はありますか?

前もって感謝します。私がはっきりしていることを願っています。

4

1 に答える 1

1

data = access_token['info']代わりに使用する必要がありますdata = access_token['user_info']

そこからdata['email]data['first_name']、 、および にアクセスできますdata['last_name']

于 2012-05-17T00:41:29.420 に答える