最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

I did a lot of googling and the best I could find was:

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?

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?

Share Improve this question edited Aug 1, 2016 at 23:16 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Jan 15, 2014 at 11:28 pavanlimopavanlimo 4,2523 gold badges33 silver badges47 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论