【问题标题】:How to retrieve full profile of linkedin user via Javascript API如何通过Javascript API检索linkedin用户的完整资料
【发布时间】:2016-06-20 08:57:50
【问题描述】:

我正在尝试通过 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,我可以通过rest api访问所有用户的信息,所以这不是访问问题;我只是不知道(也找不到任何示例)如何使用 Javascript API。我将如何指定要检索的信息以及如何在返回的 JSON 对象中识别该信息?

【问题讨论】:

    标签: javascript api linkedin


    【解决方案1】:

    使用您已注释掉的调用的变体似乎对我有用: 检查您可以使用的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...
       });
    }
    

    【讨论】:

    • 给我一个 TypeError: member.educations is undefined
    • 对于位置它有点工作,但它只返回当前位置;不是所有职位。
    • 听起来你的应用只设置了r_basicprofile 权限。在linkedin开发人员中进入您的app page,并确保您的“r_fullprofile”框已选中。
    • 由于某种原因,“r_fullprofile”甚至没有列出,即使它是一个公司帐户。
    • r_fullprofile 也未列在我的非公司帐户中。
    【解决方案2】:

    如果您想获取用户个人资料数据,那么您可以使用 javascript 修改linkedin个人资料徽章,并在没有 authtoken 和 api 的情况下获取用户数据

    LinkedIn-Profile-Data-Fetch-Without-API-Using Javascript with profile badge

    let lkList = ["devsalmanshaikh", "ravi95"];
    let linkedinid = "";
    let idx = 0;
    let badgeElm = document.getElementById("hideBadgeElm");
    let waitObserver;
    
    getData();
    
    function getData() {
      linkedinid = lkList[idx];
      badgeElm.innerHTML = `<div class="badge-base LI-profile-badge" data-locale="en_US" data-size="medium" data-theme="light" data-type="VERTICAL" data-vanity="${linkedinid}" data-version="v1"><a class="badge-base__link LI-simple-link" href="https://in.linkedin.com/in/${linkedinid}?trk=profile-badge">.</a></div>`;
      loadProfileJs();
    }
    
    function loadProfileJs() {
      var script = document.createElement("script");
      script.onload = function() {
        waitObserver = setInterval(domObserve, 1000);
      };
      script.src = "https://platform.linkedin.com/badges/js/profile.js";
      document.head.appendChild(script);
    }
    
    function domObserve() {
      let state = badgeElm.querySelector(".LI-profile-badge .profile-badge__header");
      if (state) {
        clearInterval(waitObserver);
        console.log(state);
        let json = {
          id: linkedinid,
          img: "#",
          name: "Not available",
          title: "Not available",
          info: "Not available",
        };
    
        if (badgeElm.querySelector("img.profile-badge__content-profile-image")) {
          json.img = badgeElm.querySelector("img.profile-badge__content-profile-image").src;
        }
    
        if (badgeElm.querySelector(".profile-badge__content-profile-name")) {
          json.name = badgeElm.querySelector(".profile-badge__content-profile-name").innerText;
        }
    
        if (badgeElm.querySelector(".profile-badge__content-profile-headline")) {
          json.title = badgeElm.querySelector(".profile-badge__content-profile-headline").innerText;
        }
    
        if (badgeElm.querySelector(".profile-badge__content-profile-company-school-info")) {
          json.info = badgeElm.querySelector(".profile-badge__content-profile-company-school-info").innerText;
        }
    
       
        showData(json);
      }
    }
    
    function showData(json) {
      let tbl = "<table border=1><tbody>";
      for (i in json) tbl += `<tr><td class='col0'>${i}</td><td class='col1'>${json[i]}</td></tr>`;
      output.innerHTML += tbl + "</tbody></table>";
      idx++;
      if (idx < lkList.length) getData();
    }
    <div id="output" style="padding: 12px;"></div>
    
    <div id="hideBadgeElm" style="display: none;"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2011-11-24
      • 1970-01-01
      相关资源
      最近更新 更多