LinkedIn Omniauth OAuth 2 in Rails - Authentication failure for bad redirect

1.3k views Asked by At

I am trying to figure out how to configure LinkedIn's authentication with my Rails 4 app, which uses devise and omniauth. I have: gem 'omniauth-linkedin-oauth2' in my gem file.

I have registered my application with LinkedIn and have inserted the secret and key in my app. The LinkedIn developer forum has confirmed that the process is working, but is saying that the redirect path registered does not match what I have in my callback.

I have an omniauth_callbacks controller, which has the following code in it:

def linkedin
    @user = User.find_for_linkedin_oauth(request.env["omniauth.auth"])
      if @user.persisted?
        redirect_to root_path, :event => :authentication
        # sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
        #  set_flash_message(:notice, :success, :kind => "LinkedIn") if is_navigational_format?
        else
          session["devise.linkedin_data"] = request.env["omniauth.auth"]
          redirect_to root_path
        end
      end

I am redirecting to my root_path.

My root_path is defined in my routes.rb file as: home#home.

Within my views folder, i have a folder called home, with a file called home.html.erb.

I have registered the redirect URL with LinkedIn as: www.xxxxxxx.com/home and have also tried the following: www.xxxxxxx.com, www.xxxxxxx.com/views/home/home, www.xxxxxxx.com/home/home. None of these paths work.

Please can someone help me to understand how to define a redirect path that is capable of being recognised by LinkedIn as matching my root_path.

Thank you very much in advance.

1

There are 1 answers

2
Ashitaka On BEST ANSWER

Run rake routes and see what URI pattern ends up with callback.

Most likely you will find a pattern like:

/users/auth/:action/callback(.:format)

This means that in development, your callback url will be:

http://localhost:3000/users/auth/linkedin/callback

And in production, your callback url will be:

www.xxxxxxx.com/users/auth/linkedin/callback