繁体   English   中英

使用NodeJs + PassportJS + Request获取LinkedIn连接

[英]Getting LinkedIn connections using NodeJs+PassportJS+Request

我正在创建一个需要通过Linkedin连接的webapp,我正在使用PassportJS来管理OAuth登录。 所以我设法登录,我可以检索连接用户的所有信息(用户名,标题,名字,姓氏和许多其他东西)。

但是现在下一步,我想获得当前用户的所有连接。 我有API网址来获取所有用户, https: //api.linkedin.com/v1/people/~/connections ?oauth2_access_token = XXXXXXXXXXX现在我不知道如何获得oauth2_access_token。 在LinkedIn返回的用户对象中,只有一个令牌,即x-li-auth-token,但令牌只有5个字符,这对我来说似乎有点奇怪。 我试图将令牌放在URL中,但是我收到了这个错误

<error>
  <status>401</status>
  <timestamp>1396329160196</timestamp>
  <request-id>T32OC18167</request-id>
  <error-code>0</error-code>
  <message>Invalid access token.</message>
</error>

以下是我的代码中的一些有趣内容:

在我的app.js中:

passport.use(new LinkedInStrategy({
      clientID: "xxxxxxx",
      clientSecret: "xxxxxxxx",
      callbackURL: "http://127.0.0.1:3000/auth/callback",
      scope: ['r_fullprofile', 'r_network'],
      }, function(accessToken, refreshToken, profile, done) {
        // asynchronous verification, for effect...
        process.nextTick(function () {
          return done(null, profile);
        });
      }
    ));
passport.serializeUser(function(user, done) {
  done(null, user);
});
passport.deserializeUser(function(obj, done) {
  done(null, obj);
});
app.get('/', routes.index);
app.get('/auth',
  passport.authenticate('linkedin', { state: 'SOME STATE'  }),
  function(req, res){
    // The request will be redirected to LinkedIn for authentication, so this
    // function will not be called.
  });
app.get('/auth/callback', passport.authenticate('linkedin', {
  successRedirect: '/',
  failureRedirect: '/auth'
}));
app.listen(3000);

还有我的index.js

exports.index = function(req, res) {
  var connections;
  var request = require('request');
  var options = 
      { url: 'https://api.linkedin.com/v1/people/~/connections',
        headers: { 'x-li-format': 'json' },
        qs: { oauth2_access_token: "fLz3" } // or &format=json url parameter
      };
  request(options, function ( error, r, body ) {
    if ( r.statusCode != 200 ) {
      return;
    }
    try {
      connections = JSON.parse( body );
    }
    catch (e) {
      return;
    }
  })

  res.render('index', { connections: connections, user: req.user, title: 'Bubble' });
};

您需要使用verify函数中返回的access_token

...
 }, function(accessToken, refreshToken, profile, done) {
...

暂无
暂无

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

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