在我的带有节点模块vue-recaptcha-v3
的VueJS应用中,reCAPTCHA v3在验证步骤中始终失败]。页面上应显示“受reCAPTCHA保护”的标语,并且在验证步骤之前我得到的答复还不错。当我尝试通过POST
将令牌传递给https://www.google.com/recaptcha/api/siteverify
时:
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),
});
[...]
所以我改用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).
:
mode: 'no-cors'
然后导致此响应:
[...]
const validationResponse = await fetch(validationUrl, {
method: 'POST',
mode: 'no-cors',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
[...]
我想您无法在{
"success": false,
"error-codes": [
"missing-input-response",
"missing-input-secret"
]
}
模式(no-cors
)下发送json的内容类型,所以我改用https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options作为内容类型:
multipart/form-data
但这只会引起Google的回应:
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,
});
[...]
我真的不知道该怎么办-我想念什么?
[好吧,我终于在StackOverflow上找到了答案:<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
-长话短说:Google的reCAPTCHA验证服务器似乎只接受reCAPTCHA - error-codes: 'missing-input-response', 'missing-input-secret' when verifying user's response (missing details on POST),因此在我的情况下发送的数据必须为"Content-Type": "application/x-www-form-urlencoded"
secret = $ { secretKey}&response = $ {response}`。使用这些设置,一切正常。