Javascript 如何使用Node.js和simple-oauth2对LinkedIn进行身份验证

Javascript 如何使用Node.js和simple-oauth2对LinkedIn进行身份验证,javascript,node.js,oauth,linkedin-api,Javascript,Node.js,Oauth,Linkedin Api,我正在编写一个node.js应用程序,以通过LinkedIn进行身份验证,但它不起作用。问题是,我正在重定向到(看起来是)正确的URL,但没有被转发到查询用户授权其凭据的页面,而是收到一条“未找到页面”消息 我已经创建了一个LinkedIn“应用程序”。下面是我的“授权重定向URL”: HTML <div id="root"> <button id="auth-button"> Login </button> </div>

我正在编写一个node.js应用程序,以通过LinkedIn进行身份验证,但它不起作用。问题是,我正在重定向到(看起来是)正确的URL,但没有被转发到查询用户授权其凭据的页面,而是收到一条“未找到页面”消息

我已经创建了一个LinkedIn“应用程序”。下面是我的“授权重定向URL”:

HTML

  <div id="root">
        <button id="auth-button"> Login </button>
  </div>
服务器代码

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相同

但是,我的代码没有在上图中看到提示,而是向他们提供以下信息:


您在LinkedIn中注册的
重定向uri
http://localhost:3000/callback
)与您实际发送的内容不同(
http://www.localhost:3000/callback
)。这可能是问题所在,因为它会导致
无效重定向\u uri错误

实际上这是问题的一部分,另一部分是由于某种原因,重定向默认为:?需要转到的时候:我对LinkedIn IdP不太熟悉,但我认为
重定向uri
应该是
http://localhost:8080/your-项目名称/auth/linkedin
。如果格式不正确,可能会重定向出
v2
?目前重定向正在工作。你可能是对的,按照你提到的方式练习会更好。很高兴能帮上忙。
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');
});