错误:invalid_request缺少必需参数:scope(Restify&Passportjs w / Google OAuth2)

问题描述 投票:2回答:1

所以,我使用Restify和Passportjs(Google OAuth2策略)遇到了我的Node.js应用程序的问题。当我使用passport.authenticate()时,它给我以下错误:

那是一个错误。 错误:invalid_request 缺少必需参数:范围

几年前我发现了另一个关于同样事情的问题(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);
});
javascript node.js passport.js google-oauth2 restify
1个回答
0
投票

似乎我在初始化服务器后缺少以下代码:

api.use(restify.plugins.queryParser({ mapParams: false }));

一旦我添加了该行,我就不再看到Google错误,并且该过程会传回我的代码。

© www.soinside.com 2019 - 2024. All rights reserved.