Recaptcha V3 错误不正确-captcha-sol 有时

Rah*_*sal 10 recaptcha-v3

incorrect-captcha-sol在使用 Google reCAPTCHA 服务器端验证 api 时有时会收到错误代码。

我已将 google recaptcha 验证集成到我的一些 api 中。

为此,我在来自客户端的这些 api 请求上传递了 recaptcha 令牌,然后通过遵循服务器端的 recaptcha验证服务器端对其进行验证

我通过执行以下代码获取 recaptcha 令牌并将此令牌传递给我的 api 请求标头:

  const getRecapthcaToken = () => {
   return new Promise((resolve, reject) => {
    try {
      if (window.grecaptcha && typeof window.grecaptcha.execute === "function") {
        grecaptchaExecute(window.grecaptcha.execute);
      } else {
        window.grecaptcha.ready(async () => {
          grecaptchaExecute(window.grecaptcha.execute);
        });
      }

      // grecaptcha execute action
      async function grecaptchaExecute(ExecuteAction) {
        const captchaToken = await ExecuteAction(
          xxxxxx, // my recaptcha site key
          {
            action: "submit",
          }
        );
        return resolve(captchaToken);
      }
    } catch (error) {
      return reject(error);
    }
  });
};
Run Code Online (Sandbox Code Playgroud)

然后,在服务器端我打电话https://www.google.com/recaptcha/api/siteverify?secret=${secret_key}&response=${captchaToken}

大多数时候它工作正常,但有些调用失败并且incorrect-captcha-sol错误代码随机出现。

因为它是随机发生的,所以永远不知道它何时发生以及为什么发生。请帮我弄清楚,因为在 recaptcha 文档中,我也没有关于此错误代码的任何详细信息。

小智 0

如果它是随机发生的并且大多数时候它工作正常,您可以使用try函数,因此如果它收到一个incorrect-captcha-sol它将重复,直到代码正常工作。这是代码:

try {
   function Captcha(){
      const getRecaptchaToken = () => {
      return new Promise((resolve, reject) => {
      try {
      if (window.grecaptcha && typeof window.grecaptcha.execute === "function") {
        grecaptchaExecute(window.grecaptcha.execute);
      } else {
        window.grecaptcha.ready(async () => {
          grecaptchaExecute(window.grecaptcha.execute);
        });
      }
      // grecaptcha execute action
      async function grecaptchaExecute(ExecuteAction) {
        const captchaToken = await ExecuteAction(
          APIKEY, 
          {
            action: "submit",
          }
        );
        return resolve(captchaToken);
      }
    } catch (error) {
      return reject(error);
    }
  });
};
    }
 } catch (error) {

  Captcha();
 }
Run Code Online (Sandbox Code Playgroud)