In my VueJS app with the node module vue-recaptcha-v3
, reCAPTCHA v3 constantly fails in the verification step. The "protected by reCAPTCHA" banner appears on the page like it should, and the response I get before the verification step is fine. When I try to POST
the token to https://www.google.com/recaptcha/api/siteverify
via fetch
:
// Execute reCAPTCHA with action "login".
const response = await this.$recaptcha('contact');
const data = {
secret: secretKey,
response,
};
try {
const validationResponse = await fetch(validationUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
[...]
I simply get the error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.google.com/recaptcha/api/siteverify. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
So I use mode: 'no-cors'
instead:
[...]
const validationResponse = await fetch(validationUrl, {
method: 'POST',
mode: 'no-cors',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
[...]
which then leads to this response:
{
"success": false,
"error-codes": [
"missing-input-response",
"missing-input-secret"
]
}
I suppose you can't send a content-type of json in no-cors
mode (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options), so I use multipart/form-data
as a content type instead:
const response = await this.$recaptcha('contact');
const formData = new FormData();
formData.append('secret', secret_key);
formData.append('response', response);
try {
const validationResponse = await fetch(validationUrl, {
method: 'POST',
mode: 'no-cors',
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData,
});
[...]
But this only leads to this response from Google:
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
I really don't know what to do any more - what am I missing?