Here is my session controller omniauth method
def omniauth
user=User.find_or_create_by(uid: request.env['omniauth.auth'][:uid], provider:
request.env['omniauth.auth'][:provider]) do |u|
u.email=request.env['omniauth.auth'][:info][:email]
u.password=SecureRandom.hex(15)
end
if user.valid?
session[:user_id]=user.id
redirect_to root_path
else
redirect_to sign_in_path
end
end
Here is my config/initializer/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'],
client_options: {
site: 'https://graph.facebook.com/v7.0',
authorize_url: "https://www.facebook.com/v7.0/dialog/oauth"
}
end
OmniAuth.config.allowed_request_methods = %i[get]
Here is my link_to tag for FB login
<%= link_to 'Log in with fb', '/auth/facebbok/callback', method: :post %>
Here is my route for omniauth
get '/auth/:provider/callback', to: 'sessions#omniauth'
And the problem that I was dancing is this error
enter image description here
Related
I am using oauth2 gem for google login auth. My code looks like
omniauth.rb
OmniAuth.config.logger = Rails.logger
require "omniauth-google-oauth2"
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, 'secret-client-id', 'secret-number', {client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}}}
end
my routes.rb
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
my view
<%= link_to "Sign in with Google", "/auth/google_oauth2", id: "sign_in" %>
I am getting this URL after this pages
Where am i going wrong?
Try adding token to the omniauth initializer:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, "id", "secret", {
client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}},
scope: 'email profile',
access_type: 'online',
setup: (lambda do |env|
request = Rack::Request.new(env)
env['omniauth.strategy'].options['token_params'] = {:redirect_uri => 'http://.../auth/google_oauth2/callback'}
end)
}
end
I use omniauth-google-oauth2 gem and want to get all people in my circle on google plus account, my omniauth file:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, ENV['twitter_key'], ENV['twitter_secret'], info_fields: 'followers_count'
provider :instagram, ENV['instagram_key'], ENV['instagram_secret'], info_fields: 'follows'
provider :facebook, ENV['facebook_key'], ENV['facebook_secret'], info_fields: 'friends'
google_scope = "plus.login"
provider :google_oauth2, ENV['google_key'], ENV['google_secret'], scope: google_scope, access_type: "offline"
end
but request.env['omniauth.auth'] return:
"id_info"=>
{"iss"=>"accounts.google.com",
"at_hash"=>"secret",
"aud"=>"secret",
"sub"=>"secret",
"azp"=>"secret",
"iat"=>1445275484,
"exp"=>1445279084},
"raw_info"=>
{"kind"=>"plus#personOpenIdConnect",
"gender"=>"male",
"sub"=>"103458288129240855409",
"name"=>"Sergey Naumenko",
"given_name"=>"Sergey",
"family_name"=>"Naumenko",
"profile"=>"https://plus.google.com/103458288129240855409",
"picture"=>"https://lh6.googleusercontent.com/-b8p4jlJRIm8/AAAAAAAAAAI/AAAAAAAAAV4/rZXezwwCy7w/photo.jpg?sz=50",
"locale"=>"en"}}}
I need get circledByCount
Who can help me?
I fixed it with adding option skip_friends: false
provider :google_oauth2, ENV['google_key'], ENV['google_secret'], scope: google_scope, skip_friends: false
I've set up Devise and Omniauth for users to sign in via email, twitter, and facebook. I'm not trying to allow users to tweet a message from inside the app.
I’ve got it currently working with the following code but it’s only posting from MY twitter account. I’m assuming this has to do with not setting up the Oauth_token correctly. No matter what account logins into the app, it still comes from my account.
In my User model, I have the following code (I’ve changed my key and tokens)…
def self.find_for_twitter_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
if user
return user
else
registered_user = User.where(:email => auth.uid + "#twitter.com").first
if registered_user
return registered_user
else
user = User.create(full_name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.uid+"#twitter.com",
oauth_token:auth.credentials.token,
oauth_secret:auth.credentials.secret,
password:Devise.friendly_token[0,20],
)
end
end
end
def tweet(tweet)
client = Twitter::REST::Client.new do |config|
config.consumer_key = "XXXXXXXX"
config.consumer_secret = "XXXXXXX"
config.access_token = "XXXXXXX-XXXXX"
config.access_token_secret = "XXXXXXX"
end
client.update(tweet)
end
In my config/initializer/devise.rb I have the following:
# Add Twitter OmniAuth
require 'omniauth-twitter'
config.omniauth :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
# Add Facebook OmniAuth
require 'omniauth-facebook'
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], :scope => 'basic_info, email, publish_stream'
In my view, I'm using a form for them to fill out and submit the tweet.
<p>
<%= form_for :tweet, url: tweets_path, method: :post do |f| %>
<%= f.text_field :message %>
<%= f.submit "Send Tweet" %>
<% end %>
</p>
You should authorize each twitter user with your app (as I remember with consumer key and consumer secret only).
I did this with simple way:
consumer = OAuth::Consumer.new($TWITTER_CONSUMER_KEY, $TWITTER_CONSUMER_SECRET, :site => "https://api.twitter.com")
request_token = consumer.get_request_token(:oauth_callback => "http://localhost/twitter/auth_callback")
return request_token.authorize_url
by URL I have
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier] )
token = access_token.token
secret = access_token.secret
Now if you set these toekn and secret in to your REST client you can post tweets from your user account.
I am using twitter omniauth gem in my web application. I stored my key and secret in my DB.
This is my middleware code
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, lambda { Site.config[:twitter][:key] },lambda{ Site.config[:twitter][:secret] }
end
This returns unauthorized error.
But when i specify my key and secret directly in the middleware it works.
(i.e)
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, "consumer_key" , "consumer_secret"
end
What is wrong with my first approach ?
You need to use Setup Phase
provider :twitter, :setup => true
And then in controller:
def setup
request.env['omniauth.strategy'].options[:consumer_key] = Site.config[:twitter][:key]
request.env['omniauth.strategy'].options[:consumer_secret] = Site.config[:twitter][:secret]
render :text => "Setup complete.", :status => 404
end
Routes:
match '/auth/:provider/setup' => 'sessions#setup' # for example
You can add your consumer_key and consumer secret in the development.rb and production.rb
# twitter api credential
config.twitt_consumer_key = 'xxxxxxxxxxxxxxxxx'
config.twitt_consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
And then you can use it
provider :twitter, Rails.application.config.twitt_consumer_key, Rails.application.config.twitt_consumer_secret
I want to use seperate admin login for my application using idenity provider.
I have written this in config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :identity, :model => Credential, :on_failed_registration =>SessionsController.action(:register)
provider :identity, :model => Credential, :name => 'admin', :on_failed_registration => SessionsController.action(:login_admin)
provider :google_oauth2, '000000000.apps.googleusercontent.com', '00000000000'
end
In config/routes.rb
match '/auth/admin/callback', :to => 'sessions#authenticate_admin'
In app/controllers/sessions_controller.rb
def authenticate_admin
auth_hash = request.env['omniauth.auth']
session[:admin_user] = auth_hash['user_info']['email']
if admin?
redirect_to '/'
else
render :text => '401 Unauthorized', :status => 401
end
end
But when i try to access request.env['omniauth.auth'], it always gets nil. While it is accessible when using default callback for normal users at sessison#create action. I just want to know if there is anything that has been missed in this code. I am following this blog http://www.intridea.com/blog/2011/1/31/easy-rails-admin-login-with-google-apps-and-omniauth.