erskingardner
erskingardner

Reputation: 2208

OmniAuth Invalid Response Error

I'm using OmniAuth with Devise to allow users to login with facebook or to create a normal account with a username and password. When I originally set it all up I used the excellent directions from Railscasts. Everything was working very nicely for 2+ months but just the other day the facebook login stopped working. OmniAuth sends you away to the authentication with facebook and then returns with: http://localhost:3000/auth/failure?message=invalid_response

Google has no suggestions on what causes this error or how to fix it and the OmniAuth docs don't either. I've tried digging through their code as well but the only mention of this error I've found is this, in /oa-oauth/lib/omniauth/strategies/oauth.rb:

rescue ::MultiJson::DecodeError => e
  fail!(:invalid_response, e)
end

Has anyone ever seen this error!? Know what it is or how to fix it?! This is keeping me from launching this application so any help would be very very appreciated!

Thanks, JG

Upvotes: 3

Views: 4070

Answers (5)

Mike Grayton
Mike Grayton

Reputation: 33

OK, so, sorry to post to such an old question, however having followed the Railscasts tutorial for this I was getting the same error. I have come to the conclusion that the error handling of the omniauth-twitter gem is causing the confusion, because it hides the underlying errors. I solved the problem by adding the omniauth-facebook gem to my app and authenticating with this.

This quickly uncovered the root error in my app, which was that I had put the User.create_with_omniauth method into the user controller rather than the model, a newbie error but easy to resolve.

My error was easy, and somewhat irrelevant, by using the facebook gem, the error handling allowed me to understand the problem and resolve quickly. If you are struggling with this problem, try facebook or another provider and see if you can get to the root problem more easily, and certainly avoiding some of the more complex issues such as upgrades to ruby!

Upvotes: 1

Dean Brundage
Dean Brundage

Reputation: 2048

I've been running into this error in the same situation. Devise is rescuing an unrelated Exception and handling it as an auth failure. I preempted Devise by handling the exception in the controller:

  # authentications_controller.rb
  def create
    omniauth = request.env["omniauth.auth"]
    # Blah
    # blah
    # Blark!
  rescue Exception => e
    # Just spit out the error message and a backtrace.
    render :text => "<html><body><pre>" + e.to_s + "</pre><hr /><pre>" + e.backtrace.join("\n") + "</pre></body></html>"

Upvotes: 4

ISHITOYA Kentaro
ISHITOYA Kentaro

Reputation: 347

I have had same problem and, I think I find out the solution. In Tutorial, RailsCast #235 gives authentications_controller.rb

def create
  auth = request.env["rack.auth"]
  current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'],
                                                                  auth['uid'])
  flash[:notice] = "Authentication successful."
  redirect_to authentications_url
end

but,

auth = request.env["rack.auth"]

is no longer exists in omniouth 0.2.3

auth = request.env["omniauth.auth"]

is correct.

Upvotes: 1

Eric Skiff
Eric Skiff

Reputation: 796

For anyone else that finds this via google, heroku_backup_task was the culprit for me. When we add that to our gemfile, OmniAuth decoding fails leading to this error. I assume it's some json conflict.

Not sure why it doesn't happen on 1.9.2, but I can confirm that upgrading to 1.9.2 fixes it, but can cause other issues in your app if all your gems don't play nice, and downgrading heroku appears to be a no-go. I'm going to have to destroy and re-create my app now that I've discovered the issue.

Upvotes: 2

erskingardner
erskingardner

Reputation: 2208

Ok,

I'm not sure why this has worked but it has so I'll post here in the effort to help someone else that ends up with this issue.

I upgraded my app to use ruby 1.9.2 (way of the future!) and bang, it just worked again. No idea why but hey sometimes that's just the way it goes.

Upgrading was really easy though. I was sparked into upgrade action by this dhh tweet & found this and this to be really helpful resources in making sure your 1.8.7 code will work in 1.9.2. Props to heroku as well for making it so easy to upgrade an app.

Upvotes: 1

Related Questions