首页 文章

如何通过Javascript API检索linkedin用户的完整 Profiles

提问于
浏览
1

我试图通过Javascript API检索linkedin用户的完整 Profiles (尤其是工作经历和教育资格) . 我设法将以下代码从谷歌和堆栈溢出拼凑起来:

<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对象中标识该信息?

1 回答

  • 2

    通过使用你已经注释掉的电话的变体来检查我的工作结果:检查你可以使用的fields,你在那里有"network"但是没有列出 . 也许它是旧版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...
       });
    }
    

相关问题