I am trying to retrieve the full profile (especially job history and educational qualifications) of a linkedin user via the Javascript API. I have managed to piece together the following code from google and stack overflow:
<html> <head> <title>Test</title> <script type="text/javascript" src="http://platform.linkedin.com/in.js"> api_key: blahblahblah onLoad: onLinkedInLoad authorize: true </script> <script type="text/javascript"> function onLinkedInLoad() { IN.Event.on(IN, "auth", onLinkedInAuth); } function onLinkedInAuth() { IN.API.Profile("me").result(displayProfiles); // IN.API.Profile("me").fields(["industry", "network", "date-of-birth", "educations:(id,school-name)"]).result(displayProfiles); } function displayProfiles(profiles) { member = profiles.values[0]; document.getElementById("profiles").innerHTML = "<p id="" + member.id + "">Hello " + member.firstName + " " + member.lastName + "</p>"; for(education in profiles.educations) { var id = education.id; var name = education.schoolName; console.log(name); } } </script> </head> <body> <script type="IN/Login"></script> <div id="profiles"></div> </body> </html>
This manages to retrieve the logged in user’s name and surname after they grant access. However it completely fails to retrieve anything else. I am using a company login for linkedin and I can access all the user’s information via the rest api, so it’s not an access issue; I just don’t know (and can’t find any examples) of how to use the Javascript API. How would I specify what information to retrieve and how would I then identify that information in the returned JSON object?
Advertisement
Answer
Seems to work on my end by using a variation of the call you had commented out: Check the fields you can use, you had “network” in there but it is not listed. Maybe it was part of an older version of the API?
function onLinkedInAuth() { // IN.API.Profile('me').result(displayProfiles); IN.API.Profile('me').fields([ 'first-name', 'last-name', // Add these to get the name 'industry', 'date-of-birth', 'educations:(id,school-name)', 'positions' // Add this one to get the job history ]) .result(displayProfiles); }
Then you can work with the returned data like this:
function displayProfiles(profiles) { var member = profiles.values[0]; // Note that these values are arrays and not objects var educations = member.educations.values; var positions = member.positions.values; document.getElementById('profiles').innerHTML = '<p id="' + member.id + '">Hello ' + member.firstName + ' ' + member.lastName + '</p>'; educations.forEach(function(edu) { var id = edu.id; var name = edu.schoolName; console.log(id, name); }); positions.forEach(function(position) { // Do some work with each position... }); }