代码之家  ›  专栏  ›  技术社区  ›  Hetal Chauhan

使用linkedin javascript SDK登录

  •  -2
  • Hetal Chauhan  · 技术社区  · 8 年前

    有谁能提供使用linkedin javascript SDK登录的工作示例吗?我尝试了很多演示,但都没有成功。

    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key: 81yvk0mewiwqa6
    authorize: true
    onLoad: onLinkedInLoad
    scope: r_basicprofile r_emailaddress
    </script>
    <script type="text/javascript">
    // Setup an event listener to make an API call once auth is complete
    function onLinkedInLoad() {
        alert("hi");
        IN.Event.on(IN, "auth", getProfileData);
    }
    
    // Use the API call wrapper to request the member's profile data
    function getProfileData() {
        IN.API.Profile("me").fields("id", "first-name", "last-name", "headline", "location", "picture-url", "public-profile-url", "email-address").result(displayProfileData).error(onError);
    }
    
    // Handle the successful return from the API call
    function displayProfileData(data){
        var user = data.values[0];
        document.getElementById("picture").innerHTML = '<img src="'+user.pictureUrl+'" />';
        document.getElementById("name").innerHTML = user.firstName+' '+user.lastName;
        document.getElementById("intro").innerHTML = user.headline;
        document.getElementById("email").innerHTML = user.emailAddress;
        document.getElementById("location").innerHTML = user.location.name;
        document.getElementById("link").innerHTML = '<a href="'+user.publicProfileUrl+'" target="_blank">Visit profile</a>';
        document.getElementById('profileData').style.display = 'block';
    }
    
    // Handle an error response from the API call
    function onError(error) {
        console.log(error);
    }
    
    // Destroy the session of linkedin
    function logout(){
        IN.User.logout(removeProfileData);
    }
    
    // Remove profile data from page
    function removeProfileData(){
        document.getElementById('profileData').remove();
    }
    </script>
    </head>
    <script type="in/Login"></script>
    <a href="javascript:void(0)" onclick="onLinkedInLoad();">Linked in</a>
    

    我也在尝试这一点,但我不知道问题出在哪里。

    而且

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

    此行显示linkedin按钮,但不显示任何内容! 有人能帮忙吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Hetal Chauhan    8 年前

    它不起作用,因为代码没有包装在html标记(包括头部和身体)中。

    有效的代码如下:

    <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript" src="//platform.linkedin.com/in.js">
        api_key: YOUR_API_KEY
        authorize: false
        onLoad: onLinkedInLoad
        scope: r_basicprofile r_emailaddress
        </script>
        <script type="text/javascript">
        // Setup an event listener to make an API call once auth is complete
            
        function onLinkedInLoad() {
        	
            IN.Event.on(IN, "auth", getProfileData);
        }
        
        
        // Use the API call wrapper to request the member's profile data
        function getProfileData() {
        	
            IN.API.Profile("me").fields("id", "first-name", "last-name", "headline", "location", "picture-url", "public-profile-url", "email-address").result(displayProfileData).error(onError);
        }
    
        // Handle the successful return from the API call
        function displayProfileData(data){
            var user = data.values[0];
            alert(user.emailAddress);
            alert(user.pictureUrl);
            alert(user.lastName);
            alert(user.headline);
            alert(user.location.name);
            alert(user.publicProfileUrl);
           
        }
    
        // Handle an error response from the API call
        function onError(error) {
            console.log(error);
        }
        
        // Destroy the session of linkedin
        function logout(){
            IN.User.logout(removeProfileData);
        }
        
        
        </script>
        </head>
        <body>
       <script type="in/Login"></script>
        </body>
        </html>
    推荐文章