官網:https://developer.linkedin.com/docs/oauth2
一、配置LinkedIn應用程序
a.創建一個應用 | https://www.linkedin.com/uas/login?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fdeveloper%2Fapps%3Fnewapp%3D
b.應用的基本配置
1)、JavaScript 配置如下圖 (輸入有效的網址,一般是首頁的,點擊add添加)
如果你的控制台報下面這個錯誤,就是這里沒有配置好
javascript控制台錯誤
Error: You must specify a valid JavaScript API Domain as part of this key's configuration. (你必須注冊一個有效的javascript域)
2)、Authentication 配置如下圖 (輸入當前頁面的url,點擊add)
二、初始化SDK
詳情:https://developer.linkedin.com/docs/signin-with-linkedin
官網上面寫的是必須將下面的代碼放置在<head></head>里面,而我放在dom元素下面也生效了
注:下面的代碼必須嚴格按照格式寫,
1.換行
2.不能有注釋
3.如果控制台報錯誤Error: You must specify a valid JavaScript API Domain as part of this key's configuration.就是頁面初始化sdk錯誤,一定是下面的代碼書寫有誤
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key:配置client id
authorize: true
lang: en_US
</script>
三、自定義登錄按鈕
參考 https://blog.csdn.net/zh_rey/article/details/78871365#commentsedit
<a id="link"></a>
<script>
// 處理來自API調用的成功返回
function onSuccess(data) {
//獲取數據之后填充到對應的input上
document.getElementById("firstName").value=data.values[0].firstName;
document.getElementById("lastName").value=data.values[0].lastName;
if(!data.values[0].emailAddress){
document.getElementById("email").value='';
}else{
document.getElementById("email").value=data.values[0].emailAddress;
}
}
// 處理來自API調用的錯誤返回
function onError(error) {
console.log(error);
}
// 自定義登錄監聽事件
document.getElementById("link").onclick=function(){
IN.User.authorize(getProfileData,{'r_emailaddress':'email-address'});
return false;
}
function getProfileData(data){
IN.API.Profile("me").fields("first-name", "last-name", "email-address").result(onSuccess).error(onError);
}
</script>
四、注銷
<button type="button" onclick="lingOut()">領英注銷</button>
function lingOut(){
IN.User.logout(out);
}