2012-05-17 40 views
0

我是新來的rails,我試圖整合omniauth和登錄谷歌。 衝浪在網絡上我發現了一個辦法做到這一點,但我得到這個錯誤:用谷歌開放標識訪問令牌信息設計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 

devise.rb

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

我不知道是什麼否則我可以告訴你。

對我來說,問題就在這裏:

數據= ACCESS_TOKEN [ 'USER_INFO']

的access_token [ 'USER_INFO']返回null。

我做錯了什麼?有沒有其他的方式來訪問'user_info'然後電子郵件?

在此先感謝。希望我很清楚。

回答

1

你應該只使用data = access_token['info']代替data = access_token['user_info']

從那裏您可以訪問data['email]data['first_name']data['last_name']

+0

謝謝。在家我會測試它。 ;) –

+0

謝謝!它工作完美 –

+0

這就是我們在這裏。 – janders223

相關問題