1

I am just starting to learn Node, and Express (MEAN stack) and I am wondering, can PassportJS be used to make queries to the Twitter API v1.1, or is it used solely for authentication?

I've built the following code, but I am not getting anything desirable back from it (application crashes):

My Controller:

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

Routes:

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

When I hit Localhost:3000/twitter, I end up with the following message:

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)

I'm completely new to this, I was hoping to get the auth token and then from there I can go an query the API, however, I can't get past the first step. Am I doing something wrong in my code, or am I using the Passport library incorrectly?

4

1 回答 1

0

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

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

于 2013-09-16T13:13:11.443 回答