Linkedin JavaScript SDK并不总是返回用户的电子邮件地址

时间:2018-12-05 12:44:51

标签: linkedin linkedin-api linkedin-jsapi

我正在尝试使用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(已经登录)函数定义:

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>

默认情况下为选定的应用程序权限: enter image description here

我们将为您提供快速帮助。

0 个答案:

没有答案