Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use facebook to authenticate: undefined method 'web_server' for OAuth2?

Trying to let users sign in / sign up with Twitter and Facebook. Twitter works no problem but the strategy is different for Facebook.

undefined method `web_server' for #<OAuth2::Client:0x00000005211d58>

Trace shows

oa-oauth (0.0.1) lib/omniauth/strategies/oauth2.rb:18:in `request_phase'
oa-oauth (0.0.1) lib/omniauth/strategies/facebook.rb:28:in `request_phase'
oa-core (0.0.5) lib/omniauth/strategy.rb:25:in `call!'
oa-core (0.0.5) lib/omniauth/strategy.rb:19:in `call'
oa-core (0.0.5) lib/omniauth/builder.rb:22:in `call'
warden (1.0.5) lib/warden/manager.rb:35:in `block in call'
warden (1.0.5) lib/warden/manager.rb:34:in `catch'
warden (1.0.5) lib/warden/manager.rb:34:in `call'

Anybody else experienced this?

ps. I'm using the following gems:

gem 'oa-oauth', :require => 'omniauth/oauth'
gem 'oauth2'

I'm not using the full omniauth gem as its addressable dependencies conflict with other gems.

like image 476
digitalWestie Avatar asked Aug 06 '11 15:08

digitalWestie


1 Answers

I ran into the same problem when trying to use facebook_oauth (https://github.com/moomerman/facebook_oauth) on my rails app. After spending an hour or so trying to change its code, i realized it might be easier just to use oauth2 directly. I solved the problem and now there's no need for that intermediate library. Here's how:

In Gemfile add

gem 'oauth2'

Then run

bundle update

Then, in your login_via_facebook method you either construct the dialog uri manually or use the oauth client something along these lines:

oauth_client = OAuth2::Client.new(APPLICATION_ID, APPLICATION_SECRET, {
    :authorize_url => 'https://www.facebook.com/dialog/oauth'
})

redirect_to oauth_client.authorize_url({
    :client_id => APPLICATION_ID,
    :redirect_uri => YOUR_REDIRECT_URL
})

If you need to request additional permissions, specify scope param in the authorize_url call:

redirect_to oauth_client.authorize_url({
    :client_id => APPLICATION_ID,
    :redirect_uri => YOUR_REDIRECT_URL,
    :scope => 'offline_access,email'
})

Then, in the method that handles YOUR_REDIRECT_URL (i call mine login_via_facebook_callback), do something like this:

oauth_client = OAuth2::Client.new(APPLICATION_ID, APPLICATION_SECRET, {
    :site => 'https://graph.facebook.com',
    :token_url => '/oauth/access_token'
})

begin

  access_token = oauth_client.get_token({
      :client_id => APPLICATION_ID,
      :client_secret => APPLICATION_SECRET,
      :redirect_uri => YOUR_REDIRECT_URL,
      :code => params[:code],
      :parse => :query
  })

  access_token.options[:mode] = :query
  access_token.options[:param_name] = :access_token
  facebook_user_info = access_token.get('/me', {:parse => :json}).parsed

rescue Error => e

  # You will need this error during development to make progress :)
  #logger.error(e)

end

Now facebook_user_info has the basic user info!

like image 126
jbasko Avatar answered Sep 23 '22 21:09

jbasko