简体   繁体   English

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

[英]How to authenticate LinkedIn using Node.js and simple-oauth2

I am writing a node.js app to authenticate with LinkedIn and it isn't working. 我正在编写一个node.js应用程序以通过LinkedIn进行身份验证,但无法正常工作。 The problem is that I am redirecting to (what appears to be) the correct URL, but instead of being forwarded to a page that queries the user to authorize their credentials, I get a "page not found" message. 问题是我重定向到了正确的URL(看上去是正确的URL),但是没有转发到查询用户以授权其凭据的页面,而是收到“找不到页面”消息。

I have created a LinkedIn "App". 我创建了一个领英“应用程序”。 Below are my "authorized redirect URLs": 以下是我的“授权重定向URL”:

在此处输入图片说明

HTML HTML

  <div id="root">
        <button id="auth-button"> Login </button>
  </div>

Client JS 客户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();
});

Server code 服务器代码

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');
});

When the user clicks the button they are redirected to a URL that is identical in structure to the one that is given as a "sample call" (below) in the LinkedIn developer reference. 当用户单击按钮时,他们将被重定向到一个URL,该URL在结构上与在LinkedIn开发人员参考中作为“示例调用”(如下)给出的URL相同。

https://developer.linkedin.com/docs/oauth2# https://developer.linkedin.com/docs/oauth2#

在此处输入图片说明

However instead of seeing the prompt in the image above, my code gives them this: 但是,我的代码没有看到上图中的提示,而是给了他们这些:

在此处输入图片说明

The redirect_uri you have registered in LinkedIn ( http://localhost:3000/callback ) is different to what you are actually sending ( http://www.localhost:3000/callback ). 您已在LinkedIn中注册的redirect_urihttp://localhost:3000/callback )与实际发送的内容( http://www.localhost:3000/callback )不同。 This might be the issue as it causes an invalid redirect_uri error . 这可能是问题所在,因为它会导致invalid redirect_uri error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM