javascript 是否有任何 Node.js 客户端库可以对 Twitter、Facebook、Google、LinkedIn 等进行 OAuth 和 OAuth2 API 调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21136225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:10:28  来源:igfitidea点击:

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

javascriptnode.jsoauthoauth-2.0twitter-oauth

提问by pavanlimo

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

我做了很多谷歌搜索,我能找到的最好的是: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.?

在此之上是否有任何库,它们提供包装器以对 Twitter、Facebook、Google、LinkedIn 等进行 API 调用,以说发布推文或 DM 某人或获取朋友列表或发布指向 Facebook/G+ 等的链接。 ?

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.

我知道 Passport.js,但它的使用仅限于从这些社交网站获取身份验证和授权。除此之外,目前我们将不得不通过 node-oauth 来个性化 API 调用来执行上述活动。

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

我错过了什么吗?你知道任何这样的图书馆吗?

回答by Jared Hanson

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

一旦您使用Passport.js获取访问令牌,我建议(并个人使用)请求对第三方服务进行所有 API 调用。

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 anythird party using the same, familiar module.

在我看来,特定于提供者的包装器只会增加不必要的复杂性。大多数 RESTful API 都是非常简单的 HTTP 请求。额外的层只会妨碍并添加错误以进行追踪。此外,通过坚持使用request,您可以使用相同的、熟悉的模块与任何第三方集成。

回答by Tmm

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

CloudRail可能是一个不错的选择。它为大多数社交网络提供了一个抽象的 API,并且可以很好地处理身份验证。下面是一个例子:

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);
});

回答by Sun Lee

I'm deploying Passport.jsas 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.js并且需要在身份验证之外提取额外的请求。我接受了 Jared Hanson 的“请求”建议,并使用了“请求”github 上自述文件底部的 Twitter 示例。在初始之后var request = require('request');var qs = require('querystring');这里是 Twitter 通行证身份验证和获取 follower_count 示例 - 次要请求嵌套在身份验证回调函数中:

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!

非常感谢 Jared 的慷慨帮助和创建 Passport.js!