【发布时间】:2015-02-12 11:58:17
【问题描述】:
我正在创建一个需要通过 Linkedin 连接的 webapp,我正在使用 PassportJS 来管理 OAuth 登录。所以我设法登录,我可以检索连接用户的所有信息(用户名、标题、名字、姓氏和许多其他内容)。
但现在下一步,我想获取当前用户的所有连接。我有获取所有用户的 API url,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' });
};
【问题讨论】:
标签: node.js linkedin passport.js