LinkedIn javascript API

I promised a couple posts back that I would write about how I’m doing the linked-in authorization and posting. This isn’t as easy as facebook, and there is really no good gem for it. The one that we did find (http://rubygems.org/gems/linkedin) doesn’t do posting – it’s read only.

Since I already use javascript for facebook oauth, I figured I should just follow the same path for linkedin. If you have a better method, please let me know!

So here we go:

  • Get your linked in app setup (https://www.linkedin.com/secure/developer). Get your app key & secret key, and store them as envrionment variables in your config/environment/development.rb file. I have different apps setup for dev, test, and prod so I can play with different settings. I think this is kosher with the linkedin people as long as non-prod volume is low.
  • Setup a migration to store your user’s linkedin UID and name. I don’t use these quite yet so this step is optional. I simply do it today to understand that a user has indeed been authenticated with linkedin and I may use the gem mentioned above for something later. 

https://gist.github.com/1006759

  • Ask your users for authentication. This little script does a lot, so bear with me. First, it connects to your linkedin app using your app key (i store as @liapp). Once the full linkedin script is loaded (async), it determines if the user has authorized our app or not. If so, it displays the linkedIn name (<?js= firstName ?> <?js= lastName ?>). If not, it displays a “Login with LinkedIn” button. The button opens up an authentication pop-up that requests the user to allow our webapp to write to their linkedIn wall. Upon authorization, onLinkedinAuth is automatically called which calls storeProfile passing in the profile of the authenticated user. storeProfile makes a controller call to save the uid and linkedIn name. The spans at the bottom is more control so I don’t keep asking for re-auth if the user has authorized our app before. 

https://gist.github.com/1006789

  • saveluid is the storeProfile controller call and is straightforward.

https://gist.github.com/1006810

  • Ok – so now our user’s linkedin profile is associated with our linkedin app. Woot. Now we need to post to someone’s wall when they perform some action on our site. In your controller, create the message you want to post. It should be less than 140 characters. Then embed it as a parameter to a javascript function that you will push on render :update to the page.

https://gist.github.com/1006914

  • postToLinkedIn will call a function on the page to put a JSON object with your comment in it to the user’s wall. If there’s an error, I catch it in handleLIErrors. If not, then I catch it in handleLIResults (currently just looking pretty there). handleLIErrors checks to see if the user de-authorized us via linkedin and if so, I remove their info so as to not try again until re-authorization. You’ll need your appkey script above this on the page to get it to work.

https://gist.github.com/1006916

A note about linkedin & URLs

Linkedin will autolink https urls that don’t have a self-signed certificate. That’s a bitch for your dev & QA environment which likely doesn’t meet that criteria. So, test with the prod url against your dummy linkedin profiles to get the full results. 

Also, if you use shareArticle (http://developer.linkedin.com/docs/DOC-1075) to adhoc post to walls, it will crash unless the url you reference meet those conditions above.

 

 

Good luck!

 

Leave a Reply

Powered by WordPress.com.

Up ↑

Discover more from Ctrl Alt Dad

Subscribe now to keep reading and get access to the full archive.

Continue reading