Is there any Node.js client library to make OAuth and OAuth2 API calls to Twitter, Facebook, Google, LinkedIn, etc.?

StackOverflow https://stackoverflow.com/questions/21136225

Question

I did a lot of googling and the best I could find was: https://github.com/ciaranj/node-oauth

Are there any libraries on top of this, which provide wrappers to make API calls to Twitter, Facebook, Google, LinkedIn, etc. to say post a tweet or DM somebody or get friends list or post a link to Facebook/G+ et al.?

I'm aware of Passport.js, but its usage is limited to obtaining authentication and authorization from these social sites. Beyond that, currently we will have to individualize API calls via node-oauth to perform activities mentioned above.

Have I missed something? Are you aware of any such libraries?

Was it helpful?

Solution

Once you have used Passport.js to obtain an access token, I recommend (and personally use) request to make all API calls to third-party services.

In my opinion, provider-specific wrappers just add unnecessary complication. Most RESTful APIs are very simple HTTP requests. Extra layers only get in the way and add bugs to track down. Further, by sticking with request, you can integrate with any third party using the same, familiar module.

OTHER TIPS

CloudRail might be a good alternative. It provides an abstracted API for most social networks and handles the authentication pretty well. Here is an example:

const services = require("cloudrail-si").services;
// let profile = new services.GooglePlus(redirectReceiver, "[clientIdentifier]", "[clientSecret]", "[redirectUri]", "[state]");
// let profile = new services.GitHub(redirectReceiver, "[clientIdentifier]", "[clientSecret]", "[redirectUri]", "[state]");
// let profile = new services.Slack(redirectReceiver, "[clientIdentifier]", "[clientSecret]", "[redirectUri]", "[state]");
// let profile = new services.Instagram(redirectReceiver, "[clientIdentifier]", "[clientSecret]", "[redirectUri]", "[state]");
// ...
let profile = new services.Facebook(redirectReceiver, "[clientIdentifier]", "[clientSecret]", "[redirectUri]", "[state]");

profile.getFullName((err, fullName) => {
    if (err) throw err;
    console.log("User's full name is " + fullName);
});

profile.getEmail((err, email) => {
    if (err) throw err;
    console.log("User's email address is " + email);
});

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top