Monday, March 10, 2014

Java API for Linkedin

Custom button to share in Linkedln opening a new browser window

Insert the following meta tags in the header of the html page. Linkedin will identify those tags to format the post more nicely. You DO NOT need to have the API key to do this simple task.

Insert this script first including your api key: (not mandatory: see the use of API key in linkedin developer site)

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: my_api_key
  authorize: true
</script>

<!-- Open Graph meta tags for Linkedin -->
<meta property="og:title" content="my_title" />
<meta property="og:type" content="website" />
<meta property="og:url" content="my_url" />
<meta property="og:image" content="my_image" />
<meta property="og:description" content="my_description" />
<meta property="og:site_name" content="my_site_name" />

Now add your own button where ever you want it in the html page.

<ahref="http://linkedin.com/shareArticle?mini=true&url=this_page_url" class="linkedinbtn">Linkedin</a>


Now the java script to open the window when user clicks the button:

$(".linkedinbtn").click(function() {
    var width  = 575,
    height = 450,
    left   = ($(window).width()  - width)  / 2,
    top    = ($(window).height() - height) / 2,
    url    = this.href,
    opts   = 'status=1' +
             ',width='  + width  +
             ',height=' + height +
             ',top='    + top    +
             ',left='   + left;

    window.open(this.href, "LinkedIn", opts);
    return false;
});

----- OR -------


use the following java script:

$('.linkedinbtn').click(function(){
    IN.User.authorize(onLinkedInAuth);
})

function onLinkedInAuth() {
    IN.API.Raw("/people/~/current-status")
   .method("PUT")
   .body(JSON.stringify("this_url"))
   .result( function(result) { alert("Posted to your Linkedin wall") } )
   .error(  function(error)  { alert(JSON.stringify(error)) } );
}

Enjoy using this simple way to share your links: