Error: invalid_request Missing required parameter: scope (Restify & Passportjs w/ Google OAuth2)
Error: invalid_request Missing required parameter: scope (Restify & Passportjs w/ Google OAuth2)
因此,我 运行 使用 Restify 和 Passportjs(Google OAuth2 策略)的 Node.js 应用出现问题。当我使用 passport.authenticate()
时,出现以下错误:
400. That’s an error.
Error: invalid_request
Missing required parameter: scope
几年前我发现了另一个关于同一件事的问题 (invalid_request with missing: scope using Google Passportjs on Google Oauth2)。作者说他最终自己修复了它,但没有 post 修复。
有其他人 运行 参与其中并能够修复它吗?我想不通。这是我的代码:
authController.js
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var auth = {
google: {
clientID: '<ID>',
clientSecret: '<SECRET>',
callbackURL: 'https://localhost:8080/auth/google/return',
scope: ['profile', 'email']
}
};
passport.use(new GoogleStrategy({
clientID: auth.google.clientID,
clientSecret: auth.google.clientSecret,
callbackURL: auth.google.callbackURL
}, function(accessToken, refreshToken, profile, cb) {
return cb(null, profile.id);
}));
module.exports.authenticate = passport.authenticate('google', { scope: auth.google.scope });
module.exports.isAuthenticated = passport.authenticate('google', { successRedirect: '/hello/succeed', failureRedirect: '/hello/fail' });
server.js
var restify = require('restify'),
fs = require('fs'),
bodyParser = require('body-parser'),
authController = require('./controllers/authController');
// Placeholder handler.
function respond(req, res, next) {
res.send('hello ' + req.params.name);
return next();
}
// Create server.
var api = restify.createServer({
certificate: fs.readFileSync('server.crt'),
key: fs.readFileSync('server.key'),
name: 'BQ:IQ Question Writer'
});
// Setup authentication
api.get('/auth/google', authController.authenticate);
api.get('/auth/google/return', authController.isAuthenticated);
// Setup routes.
api.get('/hello/:name', respond);
api.head('/hello/:name', respond);
api.listen(8080, function() {
console.log('%s listening at %s', api.name, api.url);
});
似乎我在初始化服务器后遗漏了以下代码:
api.use(restify.plugins.queryParser({ mapParams: false }));
添加该行后,我不再看到 Google 错误,并且该过程返回到我的代码。
因此,我 运行 使用 Restify 和 Passportjs(Google OAuth2 策略)的 Node.js 应用出现问题。当我使用 passport.authenticate()
时,出现以下错误:
400. That’s an error.
Error: invalid_request
Missing required parameter: scope
几年前我发现了另一个关于同一件事的问题 (invalid_request with missing: scope using Google Passportjs on Google Oauth2)。作者说他最终自己修复了它,但没有 post 修复。
有其他人 运行 参与其中并能够修复它吗?我想不通。这是我的代码:
authController.js
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var auth = {
google: {
clientID: '<ID>',
clientSecret: '<SECRET>',
callbackURL: 'https://localhost:8080/auth/google/return',
scope: ['profile', 'email']
}
};
passport.use(new GoogleStrategy({
clientID: auth.google.clientID,
clientSecret: auth.google.clientSecret,
callbackURL: auth.google.callbackURL
}, function(accessToken, refreshToken, profile, cb) {
return cb(null, profile.id);
}));
module.exports.authenticate = passport.authenticate('google', { scope: auth.google.scope });
module.exports.isAuthenticated = passport.authenticate('google', { successRedirect: '/hello/succeed', failureRedirect: '/hello/fail' });
server.js
var restify = require('restify'),
fs = require('fs'),
bodyParser = require('body-parser'),
authController = require('./controllers/authController');
// Placeholder handler.
function respond(req, res, next) {
res.send('hello ' + req.params.name);
return next();
}
// Create server.
var api = restify.createServer({
certificate: fs.readFileSync('server.crt'),
key: fs.readFileSync('server.key'),
name: 'BQ:IQ Question Writer'
});
// Setup authentication
api.get('/auth/google', authController.authenticate);
api.get('/auth/google/return', authController.isAuthenticated);
// Setup routes.
api.get('/hello/:name', respond);
api.head('/hello/:name', respond);
api.listen(8080, function() {
console.log('%s listening at %s', api.name, api.url);
});
似乎我在初始化服务器后遗漏了以下代码:
api.use(restify.plugins.queryParser({ mapParams: false }));
添加该行后,我不再看到 Google 错误,并且该过程返回到我的代码。