2014-04-01 33 views
0

我正在创建一个需要通过Linkedin进行连接的webapp,我使用PassportJS来管理OAuth登录。所以我设法做了登录,我可以检索连接用户的所有信息(用户名,标题,名字,姓氏和许多其他内容)。使用NodeJs + PassportJS获取LinkedIn连接+请求

但是现在下一步,我想获取当前用户的所有连接。我有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' }); 
}; 

回答

1

您需要使用在verify FUNC返回access_token回忆:

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

谢谢,我没有看到这个accessToken。 – amaurymartiny

+0

嗨疯狂我有同样的问题。你弄明白了吗? – ravisuhag