그래서 Restify 및 Passportjs (Google OAuth2 전략)를 사용하여 Node.js 앱에 문제가 발생했습니다. 내가 passport.authenticate()
를 사용하는 경우, 그것은 나에게 다음과 같은 오류를 제공합니다 :오류 : invalid_request 필수 매개 변수가 누락되었습니다 : 범위 (Google OAuth2가 포함 된 Restify 및 Passportjs)
400. That’s an error.
Error: invalid_request
Missing required parameter: scope
나는 전에 몇 년 (invalid_request with missing: scope using Google Passportjs on Google Oauth2)에서 같은 일에 대한 또 다른 질문을 발견했다. 저자는 마침내 자신을 고치 겠지만 수정 사항을 게시하지 않았습니다.
누구든지이 문제를 해결하고 고칠 수 있습니까? 나는 그것을 이해할 수 없다.
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);
});