简体   繁体   中英

How do I combine Omniauth and OAuth to sign into linkedin and retrieve API data?

I am trying create an app that will authenticate user the first time and save the user details using the LinkedIn API to the data base. The next time the user logs into linkedin, it should just login without pulling this API data out again.

To achieve this, I see there are 2 separate authorization methods, Omniauth (using the omniauth-linkedin gem) and OAuth(using the linkedin Gem). As I understand it, the Omniauth gem is used to login alone, while the Linkedin gem can be used to request and save API data.

The controller below works, but has an issue that it goes to the authentication page twice, once for the Omniauth (login) verification, and second time for the Oauth (API pull- Linkedin gem) verification.

Is there any way to combine the 2 authorizations so that the user just logs in once to linkedin and if he/ she is a first time user, it will directly authorize using OAuth as well (somehow pass an access_token from the Omniauth callback- create action to the Oauth callback)

the view

<%= link_to "Sign in with LinkedIn", "/auth/linkedin" %>

session controller.

   def create
    auth = request.env["omniauth.auth"]
    user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
    session[:user_id] = user.id
    redirect_to '/linkedin_oauth_url'
   end

   def generate_linkedin_oauth_url
   if LinkedinOauthSetting.find_by_user_id(current_user.id).nil?
     client = LinkedIn::Client.new("75xxxxxxxxxxxx", "jaXxxxxxxxxxxx")
     request_token = client.request_token(:oauth_callback => "http://#{request.host}:#{request.port}/oauth_account")
     session[:rtoken] = request_token.token
     session[:rsecret] = request_token.secret
     redirect_to request_token.authorize_url
   else
     redirect_to "/oauth_account"
   end
   end

   def oauth_account
     client = LinkedIn::Client.new("75xxxxxxxxxxxx", "jaXxxxxxxxxxxx")
     pin = params[:oauth_verifier]
     if pin
       atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
       LinkedinOauthSetting.create!(:asecret => asecret, :atoken => atoken, :user_id => current_user.id)
     end
     redirect_to "/session_index"
   end

routes.rb

  match '/oauth_account', to: "sessions#oauth_account", via: :get
  match '/linkedin_oauth_url', to: 'sessions#generate_linkedin_oauth_url', via: :get

Models (db schema)

LinkedinOauthSettings

create_table "linkedin_oauth_settings", force: true do |t|
    t.string   "atoken"
    t.string   "asecret"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Users

  create_table "users", force: true do |t|
    t.string   "provider"
    t.string   "uid"
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

I figured it out. The way to do this would be to retrieve the access key and access token from the omniauth response and passing it on to the Linkedin gem. ie the credentials of the user based on the omniauth response are saved and used to retrieve the api data by the linkedin gem. See the answer to the following question on how to retrieve the access token and secret from the omniauth response:

How do I get the access_token from the omniauth callback response?

This is not a satisfied answer please give full detail about this answer kjgekjfh oehfd uyfdgd f7efg yudgsd ufdsyufd yfguf 8tr8 dft f ef6e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM