[英]How to authenticate LinkedIn using Node.js and simple-oauth2
我正在編寫一個node.js應用程序以通過LinkedIn進行身份驗證,但無法正常工作。 問題是我重定向到了正確的URL(看上去是正確的URL),但是沒有轉發到查詢用戶以授權其憑據的頁面,而是收到“找不到頁面”消息。
我創建了一個領英“應用程序”。 以下是我的“授權重定向URL”:
HTML
<div id="root">
<button id="auth-button"> Login </button>
</div>
客戶JS
function onSignInButtonClick() {
// Open the Auth flow in a popup.
window.open('/redirect', 'firebaseAuth', 'height=315,width=400');
};
var button = document.getElementById("auth-button");
button.addEventListener("click",function(){
onSignInButtonClick();
});
服務器代碼
const credentials = {
client: {
id: "LINKEDIN_CLIENT_ID-1-2-3-4",
secret: "LINKEDIN_CLIENT_SECRET-1-2-3-4",
},
auth: {
tokenHost: 'https://www.linkedin.com/oauth/v2/authorization'
}
};
const oauth2 = require('simple-oauth2').create(credentials);
var express = require("express");
var app = express();
app.use(express.static("public"));
app.get('/', (req, res) => {
res.sendFile('landing.html',{
root:'public'
})
});
app.get('/redirect', (req, res) => {
const redirectUri = oauth2.authorizationCode.authorizeURL({
response_type:"code",
redirect_uri: "http://www.localhost:3000/callback",
state: "some-cryptic-stuff-98471871987981247"
});
res.redirect(redirectUri);
});
app.get('/callback',(req, res) => {
console.log("linkedin-callback route invoked");
res.send("linked in callback working")
});
app.listen(3000, function(err) {
console.log('Server works');
});
當用戶單擊按鈕時,他們將被重定向到一個URL,該URL在結構上與在LinkedIn開發人員參考中作為“示例調用”(如下)給出的URL相同。
https://developer.linkedin.com/docs/oauth2#
但是,我的代碼沒有看到上圖中的提示,而是給了他們這些:
您已在LinkedIn中注冊的redirect_uri
( http://localhost:3000/callback
)與實際發送的內容( http://www.localhost:3000/callback
)不同。 這可能是問題所在,因為它會導致invalid redirect_uri error
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.