omniauth-facebook Gem: omniauth.auth sometimes has nil provider and uid? - ruby-on-rails

My application is authenticating users with the omniauth-facebook gem version 1.6.0. It seems that sometimes within the facebook action in the OmniauthCallbacksController, the request.env["omniauth.auth"] hash will have nil values for provider and uid.
I would appreciate any insight into why this would happen. Here's the relevant code from my omniauth config initializer
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'],
{:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}

Related

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.
I was able to do this with the omniauth-facebook gem by modifying the devise.rb to look like the following,
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. Any help would greatly be appreciated.
#chris from omniauth-google-oauth2, in your callback controller action,
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 i.e. auth["info"]
auth
====
{
.....
"info"=>
{"name"=>"Username",
"email"=>"Useremail",
"first_name"=>"firstname",
"last_name"=>"lastname",
"image"=> "image_url",
"urls"=>{"Google"=>"https://plus.google.com/xxxxxxxxxxxxxxxxx"}
}
....
}

Omniauth Facebook doesnt return the extra data

I tried with everything that show up in google or here but doesn't work
its just show me the basic info (name,id,photo)
here s my code for omniauth.rb:
provider :facebook, 'key', 'key', {:scope => 'email',:info_fields => 'email', :client_options => {:ssl => {:ca_file => Rails.root.join("cacert.pem").to_s}}}
and this is how i call it:
auth = request.env["omniauth.auth"]
session[:omniauth] = auth

Rails - Devise+OmniAuth: how to set up credentials for staging version?

In /config/initializers/devise.rb I have something like this:
# production
config.omniauth :facebook, 'aaa', 'bbb',
:site => 'https://graph.facebook.com',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:scope => 'email'
# staging version
config.omniauth :facebook, 'ccc', 'ddd',
:site => 'https://graph.facebook.com',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:scope => 'email'
When I put these 2 blocks of code into the devise.rb file, I get the error saying that there are incorrect credentials.
I don't know what's the best approach to set up OmniAuth credentials to services like Twitter and Facebook for devise - the one I used is apparently incorrect.
What's the best approach to set up credentials for localhost, production and staging version of an app?
Thanks
It seems that your credentials are wrong for localhost. I have two versions of creditals for development and production, here is example
if Rails.env.development?
config.omniauth :facebook, "xxx", "yyy"
config.omniauth :vkontakte, "xxx_loc", "yyy_loc"
else
config.omniauth :facebook, "zzz", "rrr"
config.omniauth :vkontakte, 'zzz_loc', 'rrr_loc'
end
at /config/initializers/devise.rb
case Rails.env
when "production"
# production version
config.omniauth :facebook, 'aaa', 'bbb',
:site => GRAPH_URL,
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:scope => 'email'
when "staging"
# staging version
config.omniauth :facebook, 'ccc', 'ddd',
:site => GRAPH_URL,
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:scope => 'email'
else
# development version
config.omniauth :facebook, 'eee', 'fff',
:site => GRAPH_URL,
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:scope => 'email'
end
The best way to handle different credentials is to put them in environment variables, just like omniauth gem doc says:
https://github.com/intridea/omniauth#getting-started
Rails.application.config.middleware.use OmniAuth::Builder do
provider :developer unless Rails.env.production?
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end
This approach has several advantages:
It keeps your configuration simple
No credentials in code repository
It doesn't limit you to dev/test/prod

Get facebook app id from config/initializers/devise.rb

I've set my facebook app id in config/initializers/devise.rb and now i'm trying to retrieve it in my controller.
How do I call it back?
Devise.setup do |config|
config.omniauth :facebook, 'XXXXXX', 'XXXXX', :scope => 'email,offline_access,user_likes,user_interests,publish_actions,publish_stream'
end
Devise.omniauth_configs[:facebook].strategy.client_id

Has anyone used omniauth with rails 2.3.8?

I am new to Rails and I am trying to use omniauth with rails 2.3.8. I
couldn't find any tutorial for this version of rails so I referred to
http://blog.railsrumble.com/blog/2010/10/08/intridea-omniauth.
I added the initializer as follows:
omniauth.rb
OmniAuth::Strategies::Twitter = {
:consumer_key => 'xxxxxx',
:consumer_secret => 'xxxxxx'
}
After this step if I try to hit the URL '/auth/twitter' then I get "No
route matches "/auth/twitter" with {:method=>:get}".
Has anyone used omniauth with rails 2.3.8?
OmniOauth is a Rack::Middleware. So you need use it like that.
So you need add like that :
ActionController::Dispatcher.middleware.use OmniAuth::Strategies::Twitter = {
:consumer_key => 'xxxxxx',
:consumer_secret => 'xxxxxx'
}
This is how it works for me in rails 2.3.8
omniauth.rb:
ActionController::Dispatcher.middleware.use OmniAuth::Builder do
provider :facebook,
"key", "secret",
:scope => %(email user_birthday publish_stream offline_access),
:client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}}
end

Resources