2016-08-02 36 views
0

我有一個使用omniauth-google-oauth2 gem的rails 4.2.7應用程序設置,我想從auth哈希中檢索first_namelast_name使用omniauth-google-oauth2從auth哈希中檢索first_name和last_name gem

我能夠與omniauth-facebook寶石通過修改devise.rb看起來像下面這樣做,

devise.rb

# Omniauth/Oauth2 settings 
    callback_url = if Rails.env == "development" 
    ENV['FB_CALLBACK'] 
    else 
    ENV['FB_CALLBACK_PROD'] 
    end 

    config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], callback_url: callback_url, 
    :scope => 'email', 
    :info_fields => 'email, first_name, last_name' 
    # Google - OmniAuth 
    require 'omniauth-google-oauth2' 
    config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"] 

不過,我似乎無法弄清楚如何使用omniauth-google-oauth2檢索first_name/last_name。任何幫助將不勝感激。

+0

您所展示的只是配置。現在,當你調用'User.from_omniauth(request.env [「omniauth.auth」]'方法「時,[https://github.com/zquestz/omniauth-google-oauth2#devise]提供了響應。 –

回答

0
從omniauth - 谷歌 - 的oauth2,在回調控制器動作

@克里斯,

auth = request.env['omniauth.auth']

request.env['omniauth.auth']將包含用戶的身份驗證會話的哈希值,與基本的用戶信息在infoauth["info"]堅持

auth 
==== 
{ 
..... 
"info"=> 
    {"name"=>"Username", 
    "email"=>"Useremail", 
    "first_name"=>"firstname", 
    "last_name"=>"lastname", 
    "image"=> "image_url", 
    "urls"=>{"Google"=>"https://plus.google.com/xxxxxxxxxxxxxxxxx"} 
    } 
.... 
} 
相關問題