我正在尝试将reCAPTCHA v.3添加到我的网站注册表单中。我认为我有客户端的东西来发送请求,但是我不知道如何在服务器端处理它。
我在客户端上有这个
<script src="https://www.google.com/recaptcha/api.js?render=6LfZr7QUAAAAAEKavK4cVul9xW7a_jMzxjhALtTC"></script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js"></script>
<script src="https://www.google.com/recaptcha/api.js?&render=explicit" async defer></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('6LfZr7QUAAAAAEKavK4cVul9xW7a_jMzxjhALtTC', {action: 'homepage'}).then(function(token) {
console.log("This happened");
});
});
function onSubmit(token) {
console.log("On submit happened");
var xhr = new XMLHttpRequest();
var url = "https://us-central1-lstoday-web-client.cloudfunctions.net/verifyCaptcha";
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
console.log(json);
}
};
var data = JSON.stringify({"token": token});
console.log("Data: " + data);
xhr.send(data)
}
</script>
我在服务器端有这个
exports.verifyCaptcha = functions.https.onRequest((request, response) => {
const secret = "somethingiprobablyshouldntshow";
const url = "https://www.google.com/recaptcha/api/siteverify";
console.log(request);
const jsonstr = JSON.stringify(request)
const key = jsonstr.split("\"")
const token = key[3]
const payload = JSON.parse("{'secret': " + secret +", 'responce': " + token + "}")
console.log(payload);
request.post(url, payload,
function (error, response, body) {
if (!error && response.statusCode === 200) {
responce.send(body);
}
else {
responce.send("Failure with code " + response.statusCode + ". Error: " + error)
}
}
通过打印请求,我发现它包含很多东西,似乎都不是我在POST请求中发送的正文。如何获取POST请求(来自客户端)的正文并将其发送到reCAPTCHA进行验证?