[英]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_name
和last_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']
在info
即auth["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.