Node:可以使用Passport来进行Twitter API查询吗?

时间:2013-09-16 12:38:02

标签: javascript node.js express passport.js

我刚刚开始学习Node和Express(MEAN堆栈),我想知道,PassportJS可以用来查询Twitter API v1.1,还是仅用于身份验证?

我已经构建了以下代码,但是我没有得到任何可取的(应用程序崩溃):

我的控制器:

  /**
   * Module dependencies.
   */
  var mongoose = require('mongoose'),
      async = require('async'),
      passport = require('passport'),
      TwitterStrategy = require('passport-twitter').Strategy,
      _ = require('underscore');


  exports.auth = function(req, res, passport) {
        passport.use(new TwitterStrategy({
            consumerKey: '7rTisA540o3BkQPRItqog',
            consumerSecret: 'u5f4xShik2GSypDu7pFuXpRlf23nZdS1CeZCMSXE',
            callbackURL: "http://localhost:3000/auth/twitter/callback"
          },
          function(token, tokenSecret, profile, done) {
            console.log(token, tokenSecret, profile, done);
          }
        ));
  };

路线:

// Twitter API
app.get('/twitter', twitter.auth);

当我点击Localhost:3000 / twitter时,我最终收到以下消息:

TypeError: Object function callbacks(err) {
      var fn = route.callbacks[i++];
      try {
        if ('route' == err) {
          nextRoute();
        } else if (err && fn) {
          if (fn.length < 4) return callbacks(err);
          fn(err, req, res, callbacks);
        } else if (fn) {
          if (fn.length < 4) return fn(req, res, callbacks);
          callbacks();
        } else {
          nextRoute(err);
        }
      } catch (err) {
        callbacks(err);
      }
    } has no method 'use'
    at exports.auth (/Users/nfento/Sites/mean-stack/app/controllers/twitter.js:12:16)
    at callbacks (/Users/nfento/Sites/mean-stack/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/nfento/Sites/mean-stack/node_modules/express/lib/router/index.js:138:11)
    at pass (/Users/nfento/Sites/mean-stack/node_modules/express/lib/router/index.js:145:5)
    at Router._dispatch (/Users/nfento/Sites/mean-stack/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/Users/nfento/Sites/mean-stack/node_modules/express/lib/router/index.js:33:10)
    at Context.next (/Users/nfento/Sites/mean-stack/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Context.actions.pass (/Users/nfento/Sites/mean-stack/node_modules/passport/lib/passport/context/http/actions.js:77:8)
    at SessionStrategy.authenticate (/Users/nfento/Sites/mean-stack/node_modules/passport/lib/passport/strategies/session.js:67:10)
    at attempt (/Users/nfento/Sites/mean-stack/node_modules/passport/lib/passport/middleware/authenticate.js:243:16)

我对此完全陌生,我希望获得身份验证令牌然后从那里我可以查询API,但是,我无法通过第一步。我在代码中做错了什么,或者我是否错误地使用了Passport库?

1 个答案:

答案 0 :(得分:0)

看起来在exports.auth函数内部,passport参数不是您所期望的。您已经在应用程序中删除了大部分代码,因此不清楚您在其他地方使用该函数的方式,但似乎参数(护照库本身)未正确传递。

您可以单步执行代码并检查该参数的值是使用调试器并查找问题。有关详细信息,请参阅此问题:https://stackoverflow.com/a/3944507/1801