Archive

Archive for March 14, 2013

How to use LinkedIn Javascript API

March 14, 2013 13 comments

Before integrating with Linkedin javascript API we need to register our application with Linkedin.

To do this go to  https://www.linkedin.com/secure/developer, in the Javascript domains add your application URL, localhost is accepted

Below code will describes how to add linkedin sign in button to your application.

Sample JSP file:


<!-- 1. Include the LinkedIn JavaScript API and define a onLoad callback function -->

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

api_key: your_api_key

scope: r_network,r_emailaddress,r_fullprofile,r_basicprofile,r_contactinfo

</script>

</head>

<body>

<!-- need to be logged in to use Search; if not, offer a login button -->

<script type="IN/Login"></script>

</body>

That’s it, linkedin sign in button will appear in your page, if you click on it based on the scope it will ask your permissions.

For searching people from your application , we have to use Linkedin People Search API

To use linkedin search API, i have developed small program. It will ask for first name and last name, based on the given data it will search in linked in and prints the result in UI.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>LinkedIn JavaScript API Hello World</title>

<script src="http://code.jquery.com/jquery-1.8.2.js"></script>

<!-- 1. Include the LinkedIn JavaScript API and define a onLoad callback function -->

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

api_key: your_api_key

scope: r_network,r_emailaddress,r_fullprofile,r_basicprofile,r_contactinfo

</script>

<script type="text/javascript">

function searchClick() {

alert($("#firstNameId").val()+":"+$("#lastNameId").val());

if (!IN.ENV.auth.oauth_token) {

alert("You must login w/ LinkedIn to use the Search functionality!");

return;

}

IN.API.PeopleSearch()

.fields("id", "firstName", "lastName","emailAddress","headline","industry","pictureUrl","positions",

"summary","numConnections")

.params({

"first-name": $("#firstNameId").val(),

"last-name": $("#lastNameId").val(),

"count":25

})

.result(function(result, metadata) {

setSearchResults(result.people.values);

});

}

function setSearchResults(values) {

var table = $("#resulttable");

table.append('<tr><th>First Name</th><th>Last Name</th><th>Head Line</th><th>Industry</th><th>Picture</th><th>No Of Connections</th><th>Summary</th><th>Positions</th></tr>');

for (i in values) {

try{

var person = values[i];

var positionsStr = "<ul>";

for(i in person.positions.values){

positionsStr+="<li>"+person.positions.values[i].company.name+"</li>";

}

console.log(positionsStr);

table.append('<tr><td>'+

person.firstName+'</td><td>'+

person.lastName+'</td><td>'+

person.headline+'</td><td>'+

person.industry+'</td><td><img src="'+

person.pictureUrl+'"/></td><td>'+

person.numConnections+'</td><td>'+

person.summary+'</td><td>'+

positionsStr+'</ul></td></tr>')

}catch(err){alert(err);}

}

}

</script>

</head>

<body>

<!-- need to be logged in to use Search; if not, offer a login button -->

<div align="right">

<script type="IN/Login"></script>

</div>

<center>

<p>Basic test of the People Search API via Connect.</p>

First Name: <input type="text" name="firstName" id="firstNameId"/><br/>

Last Name: <input type="text" name="lastName" id="lastNameId"/><br/>

<input type="button" name="searchLinkedIn" value="Search LinkedIn!" onclick="searchClick();"/>

</center>

<table id="resulttable">

</table>

</body>

</html>

Thats it, for further info regarding Linkedin API http://api.linkedin.com/

Advertisements
Categories: linkedin

How to pass JSON Object string or JSONArray string from javascript to spring controller

March 14, 2013 13 comments

We usually send primitive data to spring controller by using @RequestParam annotation. But how to pass whole JSONObject string or JSONArray string to spring controller directly.

For that we have to include below jar files in buildpath

  1. jackson-core-asl-1.7.3.jar
  2. jackson-mapper-asl-1.7.3.jar

I have created Person pojo which will be mapped with javascript JSONObject exactly, Whatever the identifiers are there in this POJO should be there in Javascript JSON.

public class Person implements Serializable{
private String id;
private String firstName;
private String lastName;
//setters and getters
}

Pojo should implement Serializable interface, as  Jackson will serialize and deserialize to send data between server and client.

Our json data is :
{"persons": [
{
"firstName": "Ramesh",
"id": "id1",
"lastName": "Kotha"
},
{
"firstName": "Sathish",
"id": "id2",
"lastName": "Kotha"
}
]
}

Below is an ajax request from jsp.

$.ajax({
type: 'POST',
dataType: 'json',
contentType:'application/json',
url: "create_persons.htm",
data:JSON.stringify(arr),
success: function(data, textStatus ){
console.log(data);
//alert("success");
},
error: function(xhr, textStatus, errorThrown){
//alert('request failed'+errorThrown);
}
});

Now will see Spring controller Code:

@RequestMapping(value="/create_persons.htm")
public @ResponseBody createPerson(@RequestBody Person[] persons){
//here you can persons array as normal
for(Person person : persons){
System.out.println(person.getId());
}
}

By seeing @RequestBody annontation json data will be converted into Java Person[] and passed to persons array.

Add this in your Configuration file :

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>

That’s it, now if you pass json string to the spring controller, it will be converted into java POJO.

Categories: Java, Spring