【问题标题】:Invalid input response and secret when verifying google reCaptcha验证 google reCaptcha 时输入响应和密码无效
【发布时间】:2019-02-14 16:36:54
【问题描述】:

在向 google recaptcha api 发出发布请求时,我真的很难获得成功的响应。我收到以下回复:

{
  "success": false,
  "error-codes": [
    "invalid-input-response",
    "invalid-input-secret"
  ]
}

我查看了reCAPTCHA - error-codes: 'missing-input-response', 'missing-input-secret' when verifying user's response (missing details on POST) 并尽可能密切地关注答案,但没有成功。

下面是我的文件:

var request = require('request');

module.exports = {
  verifyCaptcha: function(req, res) {

    var secret = 'SECRET_KEY';
    var response = JSON.stringify(req.body.response);
    request({
        url: 'https://www.google.com/recaptcha/api/siteverify',
        method: 'POST',
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: `secret=${secret}&response=${response}`,
    }, function (err, response, body) {
        if (err) {
            res.status(500).send({
                error: "Could not verify captcha"
            });
        } else {
            res.status(200).send({
                message: body
            });
        }
    });
  },
}

如果有人有解决此问题的方法,请告诉我!

【问题讨论】:

    标签: javascript node.js recaptcha


    【解决方案1】:

    由于文档:https://developers.google.com/recaptcha/docs/verify

    invalid-input-secret:secret参数无效或格式错误。 可能你混合了 site_key 和 secret_key。

    【讨论】:

      【解决方案2】:

      您需要添加用户远程IP地址。

      var user_ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
      request({
          url: 'https://www.google.com/recaptcha/api/siteverify',
          method: 'POST',
          headers: { "Content-Type": "application/x-www-form-urlencoded" },
          body: `secret=${secret}&response=${response}&remoteip=${user_ip}`}...
      

      我看到您没有使用模板文字的另一件事,您应该将引号更改为 ` 而不是 '。

      或者,您应该使用现成的 reCaptcha 模块,例如:

      https://www.npmjs.com/package/recaptcha

      【讨论】:

      • 似乎没有必要为这样的东西添加一个模块。你知道为什么上面的代码不起作用吗?
      • 远程 IP 是 optional parameter。我将引号更改为 ` 但我仍然遇到同样的错误。
      • 你得到“invalid-input-response”,“invalid-input-secret”,通常意味着你的密码不正确或者你没有得到用户的输入。您是否对其进行了调试并看到您从用户那里获得了有效的响应输入?
      • 我发现了部分问题,即响应 var response = JSON.stringify(req.body.response) 不需要字符串化。所以现在我遇到的问题是“invalid-input-secret”。
      • 你的密码呢?再次检查您没有拼错。
      【解决方案3】:

      对于 reCAPTCHA Enterprise,请查看官方文档:https://cloud.google.com/recaptcha-enterprise/docs/create-assessment

      简而言之,你需要使用谷歌提供的库:

      const { RecaptchaEnterpriseServiceClient } =
              require('@google-cloud/recaptcha-enterprise');
      
      const client = new RecaptchaEnterpriseServiceClient();
      const [ response ] = await client.createAssessment({...});
      

      RecaptchaEnterpriseServiceClient 要求事先创建服务帐户,如here 所述。然后应用程序可以读取具有正确角色集的该帐户的密钥。如果无法自动检索文件,请检查构造函数的参数以查看传递数据的可用选项。

      【讨论】:

        猜你喜欢
        • 2023-02-23
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 2021-12-22
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        相关资源
        最近更新 更多