How to save from an OAuth :: Unauthorized exception in a Ruby on Rails application?

How can I get rid of an OAuth :: Unauthorized exception, as raised from OmniAuth in a Ruby on Rails application?

Obviously this:

rescue_from OAuth::Unauthorized, :with => :unauthorized 

will not work, because it only throws an exception that is thrown inside Rails, and that exception is thrown somewhere else in the stack chain.

In this application, administrators (not us, developers) set up credentials for twitter and facebook, so the presence of the wrong ones is what can and will happen. I would like to show the best message that “something went wrong” when this happens.

Refresh . I also asked in the googleuth group there are no answers yet, but if you are reading this question, you can check it out.

+3
source share
1 answer

OmniAuth runs on Rack middleware, so rescue_from will not affect it, because it is an abstraction level higher than OmniAuth through the ActionController.

This error usually occurs due to the incorrect configuration of your OAuth settings. This basically means that your application is not authorized for authentication, not for user authentication.

A configuration error is something that you, as a developer, would like to mitigate, so I'm not sure why you would like to save such an exception.

If you absolutely must eliminate this exception, you can override and use the middleware that inherits from OmniAuth

 module OmniAuth module Strategies class FacebookWithExceptionHandling < OmniAuth::Strategies::Facebook def call begin super raise OmniAuth::Unauthorized => e #handle appropriately in rack context here end end end end end Rails.application.config.middleware.use OmniAuth::Builder do provider OmniAuth::Strategies::FacebookWithExceptionHandling, api_key, #your api key secret_key, #your secret key end 
+2
source

All Articles