Search code examples
ruby-on-railsoauth-2.0deviseomniauth-linkedin

Implementing LinkedIn OAuth in Ruby on Rails using Devise and omniauth-linkedin-oauth2


I'm working on a Ruby on Rails 7 application and trying to integrate LinkedIn authentication using the Devise gem along with omniauth-linkedin-oauth2. I followed a tutorial (https://nicoproto.medium.com/sign-up-with-linkedin-on-rails-d55ea0e80f2a), but I couldn't get it to work.

Here's a brief overview of what I've done so far:

  • Added 'devise' and 'omniauth-linkedin-oauth2' gems to my Gemfile and ran bundle install.
  • Installed Devise and created a User model with Devise.
  • Configured the Devise initializer with the LinkedIn provider.
  • Updated my User model to handle the OmniAuth callback and create or update user information.
  • Added necessary fields to the User model through a migration.
  • Updated config/routes.rb to add Devise routes and the callback route.
  • Created a new controller to handle the OmniAuth callback.
  • Added a link in my view to initiate the LinkedIn OAuth flow.

Despite following these steps, I couldn't get LinkedIn authentication to work in my application. I'm looking for some guidance or a code review to help me resolve the issue.

Any help would be much appreciated. Thanks in advance!

Started POST "/users/auth/linkedin" for ::1 at 2023-04-25 13:01:39 +0200
I, [2023-04-25T13:01:39.243607 #14205]  INFO -- omniauth: (linkedin) Request phase initiated.
Started GET "/users/auth/linkedin/callback?code= A LONG STRING OF SCHARACTERS HERE" for ::1 at 2023-04-25 13:01:45 +0200
I, [2023-04-25T13:01:45.693351 #14205]  INFO -- omniauth: (linkedin) Callback phase initiated.
E, [2023-04-25T13:01:45.970601 #14205] ERROR -- omniauth: (linkedin) Authentication failure! invalid_credentials: OAuth2::Error, invalid_request: A required parameter "client_secret" is missing
{"error":"invalid_request","error_description":"A required parameter \"client_secret\" is missing"}

To sign-in with Linkedin,


Solution

  • seems the gem is currently broken omniauth-linkedin-oauth2 issue #68

    you need to add client_secret manually

    config/initializers.rb

    module OmniAuth
      module Strategies
        class LinkedIn < OmniAuth::Strategies::OAuth2
          def token_params
            super.tap do |params|
              params.client_secret = options.client_secret
            end
          end
        end
      end
    end