使用 Node.js 和 Express 构建应用程序并希望实现 reCAPTCHA。 我的代码如下:
const app = require('express')();
const bodyParser = require('body-parser');
var request = require('request-promise');
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/jow', (req, res, next) => {
console.log(req.body['g-recaptcha-response']);
var options = {
method: 'POST',
uri: 'https://www.google.com/recaptcha/api/siteverify',
body: {
secret: '6LcAuUoUAAAAAH-uiWl9cz0Wicg7iUsDxHImrgLO',
response: req.body['g-recaptcha-response'],
},
json: true // Automatically stringifies the body to JSON
};
request(options)
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log('error');
})
});
当我验证验证码并发送表单时,我得到以下输出:
错误表明我缺少输入响应(虽然我有 token ,我们可以看到已注销)并且缺少输入密码。这表明使用 request-promise
包发送的 http 请求出现了问题。我在这里做错了什么?
最佳答案
我知道这个问题已经过去很长时间了,但是,为了将来的引用,这里是解决方案。
问题出在body键上:
body: {
secret: RECAPTCHA_SECRET,
response: req.body['g-recaptcha-response']
},
当使用request-promise模块和recaptcha时,您应该使用form键。
form: {
secret: RECAPTCHA_SECRET,
response: req.body['g-recaptcha-response']
},
引用:https://github.com/request/request-promise#post-like-html-forms-do
关于node.js - 使用 Node.js 和 Express 进行 reCAPTCHA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225216/