【问题标题】:How to authenticate LinkedIn using Node.js and simple-oauth2如何使用 Node.js 和 simple-oauth2 对 LinkedIn 进行身份验证
【发布时间】:2019-04-29 20:42:39
【问题描述】:

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

我创建了一个LinkedIn“应用程序”。以下是我的“授权重定向 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');
});

当用户单击按钮时,他们会被重定向到一个与 LinkedIn 开发人员参考中作为“示例调用”(如下)给出的结构相同的 URL。

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

但是,我的代码没有看到上图中的提示,而是给了他们:

【问题讨论】:

    标签: javascript node.js oauth linkedin-api


    【解决方案1】:

    您在 LinkedIn (http://localhost:3000/callback) 中注册的 redirect_uri 与您实际发送的 (http://www.localhost:3000/callback) 不同。这可能是问题所在,因为它会导致 invalid redirect_uri error

    【讨论】:

    • 实际上这是问题的一部分,另一部分是由于某种原因重定向默认为:linkedin.com/oauth/authorize?当它需要去:linkedin.com/oauth/v2/authorization
    • 我对LinkedIn IdP不是很熟悉,但我认为redirect_uri应该是http://localhost:8080/your-project-name/auth/linkedin的形式。如果格式不正确,它可能会重定向到v2
    • 就目前而言,重定向正在工作。您可能是对的,最好按照您提到的方式进行练习。
    • 很高兴能帮上忙。
    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2013-06-24
    • 2016-10-16
    • 2013-03-06
    相关资源
    最近更新 更多