[英]How to solve responseJSON: {error: "invalid_request", error_description: "Missing required parameter: code"}
[英]Error: invalid_request Missing required parameter: scope (Restify & Passportjs w/ Google OAuth2)
因此,使用Restify和Passportjs(Google OAuth2策略)時,我的Node.js應用遇到了問題。 當我使用passport.authenticate()
,它給了我以下錯誤:
400.那是一個錯誤。
錯誤:invalid_request
缺少必需的參數:范圍
我在幾年前發現了關於同一件事的另一個問題( invalid_request缺少:在Google Oauth2上使用Google Passportjs的作用域 )。 作者說他終於自己修復了,但沒有發布修復程序。
還有其他人遇到這個問題並且能夠解決它嗎? 我不知道。 這是我的代碼:
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錯誤,該過程又傳回了我的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.