我正在使用Node.js开发一个网站(使用Express框架).为了使用Twitter身份验证,我使用passport模块(http://passportjs.org),并将他的Twitter包装器称为passport-twitter.
我的服务器端脚本是:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, passport = require('passport')
, keys = require('./oauth/keys')
, TwitterStrategy = require("passport-twitter").Strategy;
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('foo'));
app.use(express.session());
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize()); …Run Code Online (Sandbox Code Playgroud) 因为我可以req.user通过传入来获取任何路由中的登录用户:
passport.authenticate("jwt", { session: false })
Run Code Online (Sandbox Code Playgroud)
我想允许用户使用除本地登录之外的Twitter登录,因此我在node.js API中有一个passport-twitter策略.如何使用req.user访问本地登录用户?
module.exports = passport => {
passport.use(
new Strategy({
consumerKey: "",
consumerSecret: "",
callbackURL: "http://localhost:3000"
},
function(token, tokenSecret, profile, cb) {
Profile.findOne({
user: req.user._id
}).then(
userdetail => {
userdetail.twuser = profile._json.screen_name;
userdetail.token = token;
userdetail.tokenSecret = tokenSecret;
userdetail.save().then();
return cb(null, profile);
}
)
}
)
);
};
Run Code Online (Sandbox Code Playgroud) 虽然谷歌和Facebook在我的应用程序中运行良好,但我很难与twitter认证!当点击"使用推特登录"按钮时,会出现一个错误:Error: OAuthStrategy requires session support. Did you forget app.use(express.session(...))?
我在这里读到,会话是Twitter身份验证工作所必需的.我确保我的应用已激活会话!
我passport-twitter用一个简单的快速应用程序(没有风帆)测试,以确保模块正常工作,我的Twitter凭据完好无损.
我假设风帆会议不同于表达会话?帆会改变会议的工作方式吗?关于如何解决这个问题的任何建议?
编辑:在评论中添加了一些更多信息:
帆版本:0.9.13
UserController.js:
...
twitter: function(res, req) {
passport.authenticate('twitter', {failureRedirect: '/login'}, function(err, user, info) {
return console.log(err, user, info);
})(req, res);
}
...
Run Code Online (Sandbox Code Playgroud)
配置/ passport.js:
...
passport.use(new TwitterStrategy({
consumerKey: '**************',
consumerSecret: '********************',
callbackURL: "http://127.0.0.1:1337/auth/twitter/callback"
},
function(token, tokenSecret, profile, done){
process.nextTick(function() {
console.log(profile);
});
}
));
...
Run Code Online (Sandbox Code Playgroud) 我正在尝试实施 Google OAuth Passport.js 策略,但由于缺乏文档和示例,我似乎无法回答某些问题。
出于安全和技术原因,我的应用程序使用承载而不是 cookie。
Passport-google-oauth GitHub 页面上提供的单个示例并未解释某些内容,例如:
accessToken和做什么refreshToken?我真的不想使用cookie,因为这会创建第二个授权系统,该系统是已经存在的承载策略的副本。
这个问题可以应用于其他护照 OAuth 策略,例如 Twitter 和 Facebook。
oauth node.js passport-twitter passport-facebook passport.js
所以,我几乎完成了在 NestJS 支持的应用程序中实现社交登录的尝试。但我有一些问题:
首先要事。我有AuthModule并且那里有提供者TwitterGuard:
const twitterOptions: IStrategyOptionWithRequest = {
consumerKey: process.env[ENV.SOCIAL.TWITTER_CONSUMER_KEY],
consumerSecret: process.env[ENV.SOCIAL.TWITTER_CONSUMER_SECRET],
callbackURL: process.env[ENV.SOCIAL.TWITTER_CALLBACK_URL],
passReqToCallback: true,
includeEmail: true,
skipExtendedUserProfile: false,
};
export class TwitterGuard extends PassportStrategy(Strategy, 'twitter') {
constructor() {
super(twitterOptions);
}
// Magical nest implementation, eq to passport.authenticate
validate(req: Request, accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any) => void) {
const user: SocialAuthUser = {
id: profile.id,
nick: profile.username,
name: profile.displayName,
};
if (profile.emails) {
user.email = profile.emails.shift().value; …Run Code Online (Sandbox Code Playgroud) 我一直在努力让passport.js/passport-twitter使用我的项目,使用Jared Hanson的漂亮的tutoral 和它的随之而来的要点.
不用说,要点工作得很好.我只是添加我的密钥,改变我的回调网址,它只是工作.
我已将/ etc/hosts中的URL"whackypants.dev"设置为127.0.0.1.这与gist示例一起工作正常.
我的问题是,在尝试将passport,passport-twitter和connect-ensure-login与我现有的应用程序集成时,我遇到了一些相当奇怪和不一致的错误.
当我在/ login上点击"使用twitter登录"时,按照预期,我将被带到twitter并获得一个不错的oauth令牌:
http://whackypants.dev:8080/auth/twitter/callback?oauth_token=I9sGWDKRyWCJcpM3nLx3Tt3sTGRhTVYUKj03ZYIcYk&oauth_verifier=qiRQt1DGpxH2QF4zHrbKbVGj0qXTy5y2S2Sh8Ts10x7
Run Code Online (Sandbox Code Playgroud)
然而,快递抛出一个500:
Express
500 failed to fetch user profile (status: 410 data: {"errors": [{"message":
"The Twitter REST API v1 will soon stop functioning. Please migrate to API v1.1.
https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]})
at /Users/doug/whackypants/node_modules/passport-twitter/lib/passport-
twitter/strategy.js:107:30
at passBackControl (/Users/doug/whackypants/node_modules/passport-
twitter/node_modules/passport-oauth/node_modules/oauth/lib/oauth.js:374:13)
at IncomingMessage. (/Users/doug/whackypants/node_modules/passport-
twitter/node_modules/passport-oauth/node_modules/oauth/lib/oauth.js:386:9)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
Run Code Online (Sandbox Code Playgroud)
我本来希望gist示例抛出相同的错误.
甚至更奇怪:当我只使用localhost:8080/login我得到:
http://brandery-project.dev:8080/auth/twitter/callback?oauth_token=0PjNkHRMaKjG7KwlXXzUPpiFk6urANvClWK8inB58&oauth_verifier=7A2GtRUf1Q3B2HlxBVGQm4BAQIIEvdYIbBB2GZHBZ
Run Code Online (Sandbox Code Playgroud)
和一个完全不同的500错误:
Express
500 Error: failed to find request token in session
at Strategy.OAuthStrategy.authenticate (/Users/doug/whackypants/node_modules/passport-
twitter/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth.js:124:54) …Run Code Online (Sandbox Code Playgroud) 我正在我的create-react-app应用程序上设置 Twitter oauth ,方法是axios在前端使用辅助函数(via )passport在我的后端启动oauth 过程。我目前正在开发中,所以我在端口上托管我的快速服务器,在端口上托管我的3001前端,前端3000有一个代理到端口3001.我已经通过corsnpm 包设置了 CORS 权限。
无论我尝试哪种配置,我都无法完成 Twitter OAuth 流程。我试过切换端口,保持相同的端口;我试过用express-http-proxy.
我在回调函数和初始 api 调用中都使用了http://127.0.0.1而不是localhost,尝试了端口3000和3001.
我现在不确定我哪里出错了,或者我是否需要放弃passport-twitter其他解决方案。
在每种情况下,我都会收到以下错误:
Failed to load https://api.twitter.com/oauth/authenticate?
oauth_token=alphanumericcoderedactedherebyme: No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin 'http://localhost:3000' is
therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
根据我尝试的配置,我将 origin 设为nullorhttp://localhost:3001或http://127.0.0.1。
请注意,由于其他原因,例如连接到Yelp Fusion API. 此外,我使用中间件来记录我的会话数据,我可以看到我成功地获取 …
node.js ×7
passport.js ×5
express ×2
oauth ×2
api ×1
cors ×1
javascript ×1
nestjs ×1
sails.js ×1
twitter ×1