user577808
user577808

Reputation: 2527

Linkedin Oauth with Rails

We have been connecting to Linkedin for awhile now successfully. However, we get some errors from time to time and I'm hoping someone can help shed some light on this. Here's our code:

def linkedin_login
  request_token = Linkedin.client.request_token(oauth_callback: "http://#{SITE_URL}/linkedin/auth/")
  session[:linkedin_request_token] = request_token.token
  session[:linkedin_request_secret] = request_token.secret
  redirect_to request_token.authorize_url
end

def linkedin_auth
  raise "Don't have proper session or oauth_verifier" if session[:linkedin_request_token].blank? or session[:linkedin_request_secret].blank? or params[:oauth_verifier].blank?
  access_token = Linkedin.client.authorize_from_request(session[:linkedin_request_token], session[:linkedin_request_secret], params[:oauth_verifier])
  raise "Nil access token" if access_token.blank?
  redirect_to linkedin_process_path(token: access_token.first, secret: access_token.second)
end

We're hitting the "raise 'Don't have proper session or oauth_verifier'" more than I would expect. When looking at the ENV for the errors, those people don't have the session values set from the original method. We have before_filters set on the application controller so initialize the session, so I know it's active.

My next thought was whether "request_token" was generating a value request_token, and I've tried many times and they all bring something back. We get many of these a day. After the error, if the user tries again, it works fine, which is why I'm so confused.

any thoughts on what could cause this?

Upvotes: 1

Views: 1677

Answers (1)

Kamyar Mohager
Kamyar Mohager

Reputation: 709

Based on your code, it looks like you're making the request token call every time the user logs into your application. That's not the proper method to authenticate. You really only need to fetch the request token once, then use that to upgrade for an access token (as you're doing in your linkedin_auth method). From there, just save the access token and secret in your DB and fetch it anytime you need to make an API call for that particular user.

Our authentication is described more in detail here: https://developer.linkedin.com/documents/authentication

Also, this is just a personal preference, but I like using the OAuth gem for Rails as opposed to using a LinkedIn wrapper. It's easy to use and light weight.

Just as an example, you could do your auth this way:

require 'oauth'

def auth

  api_key = 'XXXXXXXXX'
  api_secret = 'XXXXXXXXX'
  configuration = { :site => 'https://api.linkedin.com',
                    :authorize_path =>   'https://www.linkedin.com/uas/oauth/authenticate',
                    :request_token_path => 'https://api.linkedin.com/uas/oauth/requestToken',
                    :access_token_path => 'https://api.linkedin.com/uas/oauth/accessToken' }

  consumer = OAuth::Consumer.new(api_key, api_secret, configuration)

  #Request token
  request_token = consumer.get_request_token

  # Output request URL to console
  puts "Please visit this URL: https://api.linkedin.com/uas/oauth/authenticate?oauth_token=" + request_token.token  + " in your browser and then input the numerical code you are provided here: "

  # Set verifier code
  verifier = $stdin.gets.strip

  # Retrieve access token object
  @access_token = request_token.get_access_token(:oauth_verifier => verifier)  
end

You would only need to invoke this method when the user first authorizes your app. Save their access token then use it for subsequent API calls. Note, my example makes use of the console to enter the PIN verifier. In a real world example you'd want to programmatically save the PIN in a session variable or in memory, then use it to get the access token.

Upvotes: 4

Related Questions