Search code examples
ruby-on-railsrubyoauthlinkedin-api

Linkedin Ruby Gem (token rejected in callback)


I've been using the LinkedIn Ruby Gem and having a problem. Here is the output from my rails server.

Started GET "/session/callback?oauth_token=618b3bc2-d8f6-4c9c-99a2-10573c8b3c3c&oauth_verifier=09858" for 127.0.0.1 at Tue Jun 12 19:14:11 -0700 2012
Processing by SessionController#callback as HTML
  Parameters: {"oauth_verifier"=>"09858", "oauth_token"=>"618b3bc2-d8f6-4c9c-99a2-10573c8b3c3c"}
Completed 500 Internal Server Error in 163ms

OAuth::Problem (token_rejected):
  app/controllers/session_controller.rb:28:in `callback'


  Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
  Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (12.9ms)

Here is the code in my session_controller.rb:

require 'linkedin'

class SessionController < ApplicationController

  def login

    # get your api keys at https://www.linkedin.com/secure/developer
    client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'], APP_CONFIG['linkedin']['secret_key'])
    request_token = client.request_token(:oauth_callback => 
                                      "http://#{request.host_with_port}/session/callback")
    session[:rtoken] = request_token.token
    session[:rsecret] = request_token.secret

    redirect_to client.request_token.authorize_url

  end

  def logout
    session[:atoken] = nil
    redirect_to :root
  end

  def callback

    client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'], APP_CONFIG['linkedin']['secret_key']) # "your_api_key", "your_secret")
    if session[:atoken].nil?
      pin = params[:oauth_verifier]
      atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
      session[:atoken] = atoken
      session[:asecret] = asecret
    else
      client.authorize_from_access(session[:atoken], session[:asecret])
    end

    redirect_to '/users/index'

  end

end

I also have random access problems, here is the errors for that:

Access Denied
You don't have permission to access "/D/16382/14334/000/origin.wwwapps.ups.com/uas/oauth/authorize?oauth_token=730c3a62-08ad-4ac2-9281-c9e67bb3a2d5" on this server.

Reference #18.6fd054b8.1339554485.72cbb3 

Solution

  • Note sure if this is the ultimate answer for this question, but one thing that helped me during development was to clear my cache and cookies. Usually this error would come up because there were cookies set from a previous session that were confusing the system and triggering this error.