也许你们可以帮我解决这个问题。我正在尝试实现 reCAPTCHA 在我的 node.js 应用程序中,无论我做什么,我都会保留 得到“invalid-site-private-key”作为响应。
以下是我反复检查并尝试过的事情:
- 正确的键
- 没有交换 key
- 键是“全局键”,因为我在 localhost 上进行测试并认为这可能是一个问题
- 在服务器上的生产环境中测试-同样的问题
我能想到的最后一件事是我对 reCAPTCHA 的 POST 请求 API 本身不正确,因为正文的具体格式不正确 明确记录(参数已记录,我知道)。所以这 是我当前正在发送的请求正文( key 和 IP 已更改 但我在我这边检查了它们):
privatekey=6LcHN8gSAABAAEt_gKsSwfuSfsam9ebhPJa8w_EV&remoteip=10.92.165.132& challenge=03AHJ_Vuu85MroKzagMlXq_trMemw4hKSP648MOf1JCua9W-5R968i2pPjE0jjDGX TYmWNjaqUXTGJOyMO3IKKOGtkeg_Xnn2UVAfoXHVQ-0VCHYPNwrj3PQgGj22EFv7RGSsuNfJCyn mwTO8TnwZZMRjHFrsglar2zQ&response=Coleshill areacce
这种格式有问题吗?我必须寄特别的吗 标题?我完全错了吗? (我连续工作了 16 个小时 现在所以这可能是..)
感谢您的帮助!
最佳答案
如上面评论中所述,我能够在 broofa 和 https://github.com/mirhampt/node-recaptcha 上的 node-recaptcha 模块的帮助下自己解决问题。 .
但首先,要完成上面缺少的细节:
- 我没有使用任何模块,我的解决方案完全是根据 the reCAPTCHA website 上提供的文档自行编写的.
我没有发送任何请求 header ,因为文档中没有任何说明。在他们解释必要的参数之前,关于请求的所有内容如下:
"After your page is successfully displaying reCAPTCHA, you need to configure your form to check whether the answers entered by the users are correct. This is achieved by doing a POST request to http://www.google.com/recaptcha/api/verify. Below are the relevant parameters."
--“如何检查用户的答案”在 http://code.google.com/apis/recaptcha/docs/verify.html
所以我自己构建了一个包含所有参数的查询字符串(它是单行的,但有一个 module,正如我现在所学的那样),并将其发送到 reCAPTCHA API 端点。我收到的只是错误代码 invalid-site-private-key
,实际上(我们现在知道)是真正发送 400 Bad Request
的错误方式。也许他们应该考虑实现这一点,这样人们就不会怀疑他们的 key 出了什么问题。
这些是明显必要的 header 参数(它们暗示您正在发送表单):
Content-Length
必须是查询字符串的长度Content-Type
必须是application/x-www-form-urlencoded
我从 node-recaptcha 学到的另一件事模块是,应该发送查询字符串 utf8
编码。
我的解决方案现在看起来像这样,您可以使用它或在它的基础上构建它,但尚未实现错误处理。它是用 CoffeeScript 编写的。
http = require 'http'
module.exports.check = (remoteip, challenge, response, callback) ->
privatekey = 'placeyourprivatekeyhere'
request_body = "privatekey=#{privatekey}&remoteip=#{remoteip}&challenge=#{challenge}&response=#{response}"
response_body = ''
options =
host: 'www.google.com'
port: 80
method: 'POST'
path: '/recaptcha/api/verify'
req = http.request options, (res) ->
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
response_body += chunk
res.on 'end', () ->
callback response_body.substring(0,4) == 'true'
req.setHeader 'Content-Length', request_body.length
req.setHeader 'Content-Type', 'application/x-www-form-urlencoded'
req.write request_body, 'utf8'
req.end()
谢谢你:)
关于http - 我的 reCAPTCHA 验证请求不断收到 "invalid-site-private-key",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450465/