I have read the omniauth oauth rdoc
#consumer = OAuth::Consumer.new(key, secret, {
:site => "http://term.ie",
:scheme => :header,
:http_method => :post,
:request_token_path => "/oauth/example/request_token.php",
:access_token_path => "/oauth/example/access_token.php",
:authorize_path => "/oauth/example/authorize.php"
})
there is no scope such as
https://graph.facebook.com/oauth/authorize?
client_id=...&
redirect_uri=http://www.example.com/callback&
scope=user_photos,user_videos,publish_stream
How do I add one? I am trying to overwrite oauth now ... do anyone got any better solution?
Put this in initializer
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook , 'app' , 'secret' , {:scope => "manage_pages"}
end
If you are using more than one scope, it's comma delimited:
:scope => "offline_access, manage_pages"
Related
I am trying to write tests for OmniAuth users and after setting up my test_helper, I am running into and a bad URI error.Sharing the details below:
test_helper.rb
# OmniAuth auth mock for testing
def setup_omniauth_mock (user)
OmniAuth.config.test_mode = true
OmniAuth::AuthHash.new ({
'provider' => 'google',
'uid' => '123545',
'user_id' => '2',
'first_name' => 'X',
'last_name' => 'XYZ',
'email' => 'xxyz#example.com',
'image' => 'https://lh3.googleusercontent.com//photo.jpg',
'oauth_token' => 'abcdef12345',
'oauth_expires_at' => DateTime.now,
})
OmniAuth.config.add_mock(:google, OmniAuth::AuthHash.new)
get '/auth/":google"/callback'
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
get '/auth/:google/callback'
end
The error I am getting:
test_validating_a_Google_OAuth_user#SessionsControllerTest (0.49s)
URI::InvalidURIError: URI::InvalidURIError: bad
URI(is not URI?): http://www.example.com:80/auth/":google"/callback
test/test_helper.rb:42:in `setup_omniauth_mock'
Now I followed the documentation here [Oauth Integration Testing][1]
[1]: https://github.com/omniauth/omniauth/wiki/Integration-Testing but I think there is something I am doing wrong.
Can someone please help me guide through this.
Thank you!
J.
I actually resolved it by cleaning things a bit.
My test_helper.rb now:
# OmniAuth auth mock setup for testing
setup do
OmniAuth.config.test_mode = true
Rails.application.env_config["omniauth.auth"] =
OmniAuth.config.mock_auth[:google]
end
#teardown OmniAuth mock setup
teardown do
OmniAuth.config.test_mode = false
end
#Google OAuth mock
def google_oauth2_mock (user)
OmniAuth.config.mock_auth[:google]
OmniAuth::AuthHash.new ({
'provider' => 'google_oauth2',
'uid' => '123545',
'user_id' => '2',
'first_name' => 'X',
'last_name' => 'XXYZ',
'email' => 'xxyzjam#example.com',
'image' => 'https://lh3.googleusercontent.com/photo.jpg',
'oauth_token' => 'abcdef12345',
'refresh_token' => '12345abcdef',
'oauth_expires_at' => DateTime.now,
})
end
I put the routes in the individual tests, and that allowed me to run the tests suite smoothly.
Hope I am able to save you some time and frustrations.
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
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'}}}
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
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