google 重新验证 returns 由于 "invalid-input-secret" 错误

google recaptcha returns false due to "invalid-input-secret"

我已经安装了来自 google 的最新 recaptcha,但它在 post 提交时总是 returns 错误,并且总是返回 "invalid-input-secret" 错误,即使验证总是正确的前端视图。这可能是什么原因。顺便说一句,我正在使用 phalcon 框架测试本地主机 xampp 中的所有内容。这是我检查验证码的部分:

protected function validate_data($post, $ip){

    $validation = new Validation();
    $validation->add(
        'user_name',
        new PresenceOf(
            array(
                'message' => 'The Username is required'
            )
        )
    );
    $validation->add(
        'email',
        new PresenceOf(
            array(
                'message' => 'The e-mail is required'
            )
        )
    );
    $validation->add(
        'password',
        new PresenceOf(
            array(
                'message' => 'The Password is required'
            )
        )
    );
    $validation->add(
        'cpassword',
        new PresenceOf(
            array(
                'message' => 'The Confirmation Password is required'
            )
        )
    );
    $validation->add(
        'email',
        new Email(
            array(
                'message' => 'The e-mail is not valid'
            )
        )
    );

    $validation->add('password', 
        new Confirmation(array(
           'message' => 'Password doesn\'t match confirmation',
           'with' => 'cpassword'
           )
        )
    );

    $error = $validation->validate($post);
    $errors = array();
    if(count($error)){
        foreach($error as $e){
            $errors[] = $e;
        }
    }

    $data = array(
        'secret' => "my secret key",
        'response' => $post['g-recaptcha-response'],
        'remoteip' => $ip
    );

    $verify = curl_init();
    curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($verify, CURLOPT_POST, true);
    curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($verify);

    $captcha = json_decode($res);

    if($captcha->success == false){
        $errors[] = "Invalid Captcha, You are a freakin robot!";
    }

    return $errors;

}

这可能是什么原因?这是转储响应时的输出:

object(stdClass)#70 (2) { ["success"]=> bool(false) ["error-codes"]=> array(1) { [0]=> string(20) "invalid-input-secret" } } 

愚蠢的我,我仔细检查了密钥,它只是在开头缺少一个字符。那解决了。

对于javascript

在我的例子中,我使用了环境变量 我用双引号"6Lfg1_0UAAAAABWdUn5gNhXEuLxhpkQyheDpLbnB"定义了变量。因此,当密钥通过 url 传递时,它将包含在双引号中。这是我的问题。 url.

中不应包含双引号

定义环境变量时用单引号定义如下:

RECAPTCHA_SECRET_KEY='6Lfg1_0UAAAAABWdUn5gNhXEuLxhpkQyheDpLbnB'

整个问题的出现是因为 Google 的混乱窥视。他们创建了多个渠道来实施 reCAPTCHA 注册。如果您遇到此错误,我相信您已经按照第一种方法为您的 reCAPTCHA 应用程序生成了 SECRET KEY。

  1. 您访问网站:https://console.cloud.google.com/security/recaptcha 并为您的网站生成 reCAPTCHA 密钥。请注意,这是一个 Google 云控制台 link,您必须只收到一个用于前端和后端的密钥。

  2. 您访问网站:https://www.google.com/recaptcha/admin/create 并为您的应用程序生成 reCAPTCHA 密钥。请注意,这不是 Google 云控制台网站,而是一个单独的网站,它将为您提供 2 个不同的密钥,一个用于您的后端,另一个用于您的前端。使用这些键,你应该可以开始了。

这对 Google 来说很糟糕,因为他们有多个来源用于同一个目标设施,其中一种方法有点被弃用或不起作用。

Sarthik Gupta 回答的补充:

此问题是由于您要使用的 recaptcha 帐户类型所致:

  1. https://www.google.com/recaptcha/admin/create is for an enterprise account, so in that case, once the client key is created, authentication should be performed using an API key created in the google cloud console, or a service account, as described here https://cloud.google.com/recaptcha-enterprise/docs/using-features?_ga=2.231710239.-1292418016.1646407998#migrate-backend
  2. https://www.google.com/recaptcha/admin/create 用于免费帐户,在这种情况下,客户端和服务器密钥都已创建并可直接使用