我正在尝试通过Javascript API检索linkedin用户的完整个人资料(特别是职位历史和教育资格)。我设法将以下代码从谷歌和堆栈溢出拼凑起来:
<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>
设置在授予访问权限后检索登录用户的姓名和姓氏。但是它完全无法检索任何其他内容。我使用公司登录来获取linkedin,我可以通过其余的api访问所有用户的信息,因此这不是访问问题;我只是不知道(并且找不到任何示例)如何使用Javascript API。如何指定要检索的信息以及如何在返回的JSON对象中标识该信息?
答案 0 :(得分:2)
通过使用您注释掉的调用的变体,似乎可以在我的最终工作: 检查您可以使用的fields,那里有“网络”,但未列出。也许它是旧版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);
}
然后你可以使用这样的返回数据:
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...
});
}