我正在尝试使用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>
我们将为您提供快速帮助。