gpt4 book ai didi

javascript - Node : Can Passport be used to make Twitter API queries?

转载 作者:太空宇宙 更新时间:2023-11-04 02:38:59 24 4
gpt4 key购买 nike

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

我已经构建了以下代码,但我没有从中得到任何想要的东西(应用程序崩溃):

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

路线:

// 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)

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

最佳答案

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

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

关于javascript - Node : Can Passport be used to make Twitter API queries?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18828295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com