0

I'm attempting to add Facebook connect to our web app, and I'm running into a problem with. Everything works fine locally (I can authenticate through Facebook), but when I push the code to our dev server (which lives in the wild), every time I try to authenticate it returns the following error code:

OAuth2::HTTPError: Received HTTP 400 during request

That's really the only explanation I'm getting. Again, this works on my local machine, and the gems and such match between boxes, so I'm a bit confused. Here's the code I'm executing.

def facebook_connect
  #Set the scope we want to pull from Facebook, along with the callback URL
  options = {
    :redirect_uri => facebook_callback_url,
    :scope => "email,publish_stream"
  }

  #Go out and fetch the url
  client = OAuth2::Client.new(FACEBOOK_API_KEY, FACEBOOK_SECRET, {:site => FACEBOOK_API_URL, :access_token_method => :post})
  #Redirect to the callback for processing
  redirect_to client.web_server.authorize_url(options)
end

def facebook_callback
  #Client URL
  client = OAuth2::Client.new(FACEBOOK_API_KEY, FACEBOOK_SECRET, {:site => FACEBOOK_API_URL, :access_token_method => :post})

  #Parse out the access token
  access_token = client.web_server.get_access_token(params[:code], :redirect_uri => facebook_callback_url)

  #Get the user
  fb_user = JSON.parse(access_token.get('/me'))

  #Do some authentication database stuff
end

def facebook_callback_url
  uri = URI.parse(request.url)
  uri.path = '/users/facebook_callback'
  uri.query = nil
  uri.to_s
end

I searched Google, but the solutions that show up aren't working. Also, if anyone knows how to parse and display OAuth2 errors, I would appreciate that, as well. Thanks

Kevin Whitaker
  • 12,435
  • 12
  • 51
  • 89

2 Answers2

0

Assuming that Facebook OATH knows of your server's IP address(they are very strict about it), I would recommend that you use use 'rescue' to catch that exception, get the backtrace and then find where it is being raised and place a bunch of debug statements to check the state of both request and the response, as well as access tokens.

Or you can configure remote debugging with Rubymine or NetBeans which is not an easy task :)

Vlad Gurovich
  • 8,463
  • 2
  • 27
  • 28
  • That's part of the problem - I don't know how to catch/display the error/exception. – Kevin Whitaker May 07 '11 at 00:58
  • if its not apparent to you after which line the exception gets thrown, put a lot of debug statements in. I suspect its your access_token.get call. wrap it in begin/rescue and do a backtrace on the error – Vlad Gurovich May 07 '11 at 01:04
0

The issue actually ended up being a problem with the "Faraday" gem. Our dev server wasn't set up to handle SSL, which was returning an error code. We patched it using the following answer:

OmniAuth & Facebook: certificate verify failed

Community
  • 1
  • 1
Kevin Whitaker
  • 12,435
  • 12
  • 51
  • 89