Search code examples
ruby-on-rails-4oauth-2.0linkedin-api

Production Linkedin Oauth


I'm having an awkward problem using Linkedin gem.

My app tries to get the user data using oauth. On development enviroment it works just fine. But as soon as I upload it to heroku it starts to give me errors like:

2014-07-17T04:25:07.552779+00:00 app[web.1]: NoMethodError (undefined method `picture_urls' for #<LinkedIn::Client:0x007f4f4cea65f8>):
2014-07-17T04:25:07.552783+00:00 app[web.1]:   app/models/social_auth.rb:7:in `fetch_details'

The real problem is that it used to work and stopped from nowhere. I even gave a rollback on my git repositories to see if the past code is working but its not.

It seems like it does not answer me correctly even giving the correct callback (I'm sure the error is happening after the oauth validation).

Check the complete code of the definition (that is executed after the model save):

def fetch_details_from_linkedin
    client = LinkedIn::Client.new(ENV["LINKEDIN_KEY"], ENV["LINKEDIN_SECRET"])
    authkeys = [self.token, self.secret]
    client.authorize_from_access(authkeys)

    @linkedin = LinkedinUserData.where(user: self.user).first_or_initialize
    @linkedin.avatar_url = client.picture_urls.all.first
    @linkedin.profile_url =  client.profile.site_standard_profile_request.url

    @linkedin.save
end

Each item on this model have the token and secret used to validate the user with the 'authorize_from_access' method. I don't know why, but it seems that the callback is answering me an empty object.

Things I already thought about: Live status on API - CHecked. It gives the error no matter the status of the app on the linkedin api

Callback URL: already checked, even with https. As I said, the callback is being executed. The error is an internal error (500).


Solution

  • I just contacted the developer of the gem and together we discovered that even not specifying the version, heroku for some reason was getting an old version of the gem since github was already on 0.4.7 and it was getting a 0.2.x.

    So, I solved this problem by specifying the gem version. Don't know what is happening on heroku, but at least the problem is solved and linkedin is authorizing again.

    gem 'linkedin', '0.4.7'
    

    ps: if you get an dependency error, just declare in your gem file:

    gem 'hashie', '2.0'
    

    This happens because of an ominiauth dependency.