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

3

13

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?

Mooch answered 15/1, 2014 at 11:28 Comment(0)
M
23

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.

Mediate answered 16/1, 2014 at 15:39 Comment(4)
I am using passport.js for facebook and twitter is there any way i can get facebook and twitter friends using it ?Forbid
I think restler is also a good lib to use in place of request.Multifarious
Having to reinvent the wheel every time you want to want to use a new API service is a waste of timeWhitewall
I personally use Purest as a generic REST API library. It's built on top of request but have features specific to REST APIs. Also I'm using Grant as alternative for obtaining access tokens.Waxman
S
3

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);
});
Spindrift answered 9/8, 2016 at 14:13 Comment(0)
P
1

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!

Pitcher answered 29/4, 2014 at 3:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.