I'm deploying Passport.js as well and needed to pull extra requests beyond authentication.
I took Jared Hanson's 'request' suggestion and used the Twitter example found towards the bottom of the README at the 'request' github. After the initial var request = require('request');
and var qs = require('querystring');
here is the Twitter passport authenticate & get followers_count example - the secondary request is nested inside the authentication callback function:
passport.use(new TwitterStrategy({
// var configAuth = require('./auth');
consumerKey : configAuth.twitterAuth.consumerKey,
consumerSecret : configAuth.twitterAuth.consumerSecret,
callbackURL : configAuth.twitterAuth.callbackURL,
passReqToCallback : true
},
function(req, token, tokenSecret, profile, done) {
process.nextTick(function() {
if (!req.user) {
User.findOne({ 'twitter.id' : profile.id }, function(err, user) {
if (err)
return done(err);
if (user) {
if (!user.twitter.token) {
user.twitter.token = token;
user.twitter.tokenSecret = tokenSecret;
user.twitter.username = profile.username;
user.twitter.displayName = profile.displayName;
// [ADDED] Twitter extended API calls using 'request' and 'querystring'
var oauth = {
consumer_key : configAuth.twitterAuth.consumerKey,
consumer_secret : configAuth.twitterAuth.consumerSecret,
token : token,
token_secret : tokenSecret
}
var url = 'https://api.twitter.com/1.1/users/show.json?';
var params = {
user_id: profile.id
}
url += qs.stringify(params)
request.get({url:url, oauth:oauth, json:true}, function (e, r, result) {
// Get followers_count here
user.twitter.followers = result.followers_count;
// [MOVED] db.save moved into second callback function
user.save(function(err) {
if (err)
throw err;
return done(null, user);
});
});
// [END ADD]
}
return done(null, user);
} else {
var newUser = new User();
newUser.twitter.id = profile.id;
newUser.twitter.token = token;
newUser.twitter.tokenSecret = tokenSecret;
newUser.twitter.username = profile.username;
newUser.twitter.displayName = profile.displayName;
// [ADDED] Twitter extended API calls using 'request' and 'querystring'
var oauth = {
consumer_key : configAuth.twitterAuth.consumerKey,
consumer_secret : configAuth.twitterAuth.consumerSecret,
token : token,
token_secret : tokenSecret
}
var url = 'https://api.twitter.com/1.1/users/show.json?';
var params = {
user_id: profile.id
}
url += qs.stringify(params)
request.get({url:url, oauth:oauth, json:true}, function (e, r, result) {
// Get followers_count here
newUser.twitter.followers = result.followers_count;
// [MOVED] db.save moved into second callback function
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
});
// [END ADD]
}
});
} else {
var user = req.user;
user.twitter.id = profile.id;
user.twitter.token = token;
user.twitter.tokenSecret = tokenSecret;
user.twitter.username = profile.username;
user.twitter.displayName = profile.displayName;
// [ADDED] Twitter extended API calls using 'request' and 'querystring'
var oauth = {
consumer_key : configAuth.twitterAuth.consumerKey,
consumer_secret : configAuth.twitterAuth.consumerSecret,
token : token,
token_secret : tokenSecret
}
var url = 'https://api.twitter.com/1.1/users/show.json?';
var params = {
user_id: profile.id
}
url += qs.stringify(params)
request.get({url:url, oauth:oauth, json:true}, function (e, r, result) {
// Get followers_count here
user.twitter.followers = result.followers_count;
// [MOVED] db.save moved into second callback function
user.save(function(err) {
if (err)
throw err;
return done(null, user);
});
});
// [END ADD]
}
});
}));
Many thanks to Jared for being very generous with his help and for creating Passport.js!