简体   繁体   English

使用omniauth-google-oauth2 gem从auth哈希中检索first_name和last_name

[英]retrieve first_name and last_name from auth hash using omniauth-google-oauth2 gem

I have a rails 4.2.7 app setup using the omniauth-google-oauth2 gem, and I would like to retrieve the first_name and last_name from the auth hash. 我有一个使用omniauth-google-oauth2 gem的Rails 4.2.7应用程序设置,我想从auth哈希中检索first_namelast_name

I was able to do this with the omniauth-facebook gem by modifying the devise.rb to look like the following, 通过修改devise.rb使其类似于以下内容,我可以使用omniauth-facebook gem做到这一点,

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"]

However, I can't seem to figure out how to retrieve the first_name / last_name using omniauth-google-oauth2 . 但是,我似乎无法弄清楚如何使用omniauth-google-oauth2检索first_name / last_name。 Any help would greatly be appreciated. 任何帮助将不胜感激。

@chris from omniauth-google-oauth2, in your callback controller action, 来自omniauth-google-oauth2的@chris,在您的回调控制器操作中,

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

request.env['omniauth.auth'] will contain a hash of users' auth session, with which basic user info are persisted in info ie auth["info"] request.env['omniauth.auth']将包含用户的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"}
  }
....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 facebook omniauth 获取 first_name 和 last_name 字段 - get first_name and last_name fields from facebook omniauth 如何使用fb_graph2从facebook检索用户的电子邮件,first_name和last_name? - How to retrieve an user's email, first_name and last_name from facebook with fb_graph2? 尝试按 first_name 和 last_name 搜索 - Trying to search by first_name and last_name 按名字和姓氏搜索数据 - Search data by first_name and last_name 在Rails 2.3.8和ruby 1.8.7中使用omniauth-google-oauth2 gem - Using omniauth-google-oauth2 gem in rails 2.3.8 and ruby 1.8.7 如何从belongs_to关联轨访问first_name和last_name - How to access first_name and last_name from belongs_to assocation rails 使用omniauth-google-oauth2 gem进行设计 - Devise with omniauth-google-oauth2 gem 当我尝试使用omniauth-google-oauth2 gem进行身份验证时,没有路由匹配[GET]“/ auth / google_apps” - No route matches [GET] “/auth/google_apps” when I try to authenticate with omniauth-google-oauth2 gem 在 rails API 中使用 omniauth-google-oauth2 - Using omniauth-google-oauth2 in rails API 使用Ransack搜索部分全名,其中first_name和last_name是数据库列 - Search partial full_name with Ransack where first_name and last_name are database columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM