reCaptcha后端nodejs

时间:2018-03-30 20:13:03

标签: node.js recaptcha backend

我只想使用NodeJS验证reCAPTCHA,并且无法进行简单的调用!

我不断收到错误missing-input-responsemissing-input-secret

使用request尝试1:

var request = require('request');

...

request.post(
    'https://www.google.com/recaptcha/api/siteverify',
    {
        secret: 'MY_SECRET',
        response: recaptcha
    },
    function (error, response, body) {
        // guard
        if (error) {
            callback(false);
            return;
        }

        if (response.statusCode == 200) {
            console.log("BODY", body)
            if (body.success) {
                callback(true);
            } else {
                callback(false);
            }
        }
    }

使用https尝试2:

var post_req;

var requestBody = {
    secret: 'MY_SECRET',
    response: recaptcha
};

post_req = https.request('https://www.google.com/recaptcha/api/siteverify', function (res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('CHUNK: ', chunk);
    });
});

post_req.on('error', function (e) {
    console.log('ERROR: ', e);
    callback(false);
});

post_req.write(requestBody);

post_req.end();

结果是:

{
    "success": false,
    "error-codes": [
        "missing-input-response",
        "missing-input-secret"
    ]
}

1 个答案:

答案 0 :(得分:0)

终于找到了解决方案。似乎问题出在Content-Type上。

这有效:

var request = require('request');

...

request.post(
    'https://www.google.com/recaptcha/api/siteverify',
    {
        form: {
            secret: 'MY_SECRET',
            response: recaptcha
        }
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);