首页 > 解决方案 > Linkedin JavaScript SDK 并不总是返回用户的电子邮件地址

问题描述

我正在尝试使用 LinkedIn JavaScript SDK 在我的网站上集成 LinkedIn 登录功能,但它并不总是返回用户的电子邮件地址。有时会,有时不会。

这是我的代码。

加载 API:

<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key:   MY_API_KEY
authorize: true
onLoad: onLinkedInLoad 
</script>

这个函数在 API 加载时被调用:

function onLinkedInLoad() {

    /*
    * Defining the scope
    */
    var scope = {scope: 'r_emailaddres'};

    /*
    * If the user is not authorized, attach a click event hander with .li-login button
    */
    if( !IN.User.isAuthorized() ) {

        $('.li-login').on('click', function( event ){
            event.preventDefault();

            IN.User.authorize(function(){
                getProfileData( false );
            }, scope);  
        });
    }
    else{

        IN.Event.on(IN, "auth", function(data){

            console.log(data)
            getProfileData( true );
        });
    } 
}

getProfileData( alreadyLoggedin ) 函数定义:

function getProfileData( alreadyLoggedin) {

    if(alreadyLoggedin){
        IN.API.Raw("/people/~:(id,firstName,lastName,emailAddress,picture-url)").result(function(response){

            $('.li-modal .user-fullname').html(response.firstName+' '+response.lastName);
            $('.li-modal img').attr('src', response.pictureUrl);
            $('.li-login').removeClass('li-login-btn'); 


            console.log(response)

        }).error(onError);
    }
    else{
        IN.API.Raw("/people/~:(id,firstName,lastName,emailAddress,picture-url)").result(function( response ){
            console.log(response);
            var data = {
                "first_name": response.firstName,
                "last_name": response.lastName,
                "email": response.emailAddress,
                "service": 'linkedin',
                "picture": response.pictureUrl,
            };  

            //attemptLogin(data); 

        }).error(onError);
    }

}

登录按钮代码:

<a href="#" class="li-login"><img src="images/linkedin-icon.png"></a>

默认选择应用程序权限: 在此处输入图像描述

快速帮助将不胜感激。

标签: linkedinlinkedin-apilinkedin-jsapi

解决方案


推荐阅读