reCAPTCHA Enterprise 令牌格式错误且站点密钥无效

KJH*_*son 5 recaptcha google-cloud-platform

我正在尝试将 reCAPTCHA enterprise 集成到我的 Flask Web 应用程序的登录页面上。

最初,我将 reCAPTCHA 复选框添加到登录页面,并通过 POST 请求“g-recaptcha-reponse”参数检索令牌,如文档中所述

但是,在检查令牌是否有效时,我不断收到错误,评估响应对象中显示“MALFORMED”,如下所示......

# main Python code to get the token from the post request
recaptchaToken = request.form.get("g-recaptcha-response")

# response from Google using google-cloud-recaptcha-enterprise Python library
# action name: "projects/<project_id>/assessments/<action_id>" # Note that I hid the project_id and action_id for this question
# event {
#   token: "AW-bC4mm022xpjWh2zI7od65_YdX81M6XfBPW5wIJtr8s5XXIzZ6ojfTFl9PjRlDHo601Svqpuyn5E8WhDlcBswXnrPGDbtUHekXcw60deyb8w0wyk1gZA0mJ0Hx9eAhGMI"
#   site_key: "6LfpqZcgAAAAAC7RH7qroayHutXeXkpLuKY5iV6a"
# }
# token_properties {
#   invalid_reason: MALFORMED
#   create_time {
#   }
# }
Run Code Online (Sandbox Code Playgroud)

然后我决定寻找其他解决方案,例如将令牌数据附加到表单grecaptcha.enterprise.execute并将表单数据提交到后端,但我在控制台中收到另一个错误,如下所示......

// js code
grecaptcha.enterprise.ready(function() {
    grecaptcha.enterprise.execute("6LfpqZcgAAAAAC7RH7qroayHutXeXkpLuKY5iV6a", {action: "login"}).then(function(token) {
        console.log(token);
    });
});

// Error in console
// Uncaught (in promise) Error: Invalid site key or not loaded in api.js: 6LfpqZcgAAAAAC7RH7qroayHutXeXkpLuKY5iV6a
//     at Array.<anonymous> (recaptcha__en.js:113:393)
//     at recaptcha__en.js:62:400
Run Code Online (Sandbox Code Playgroud)

此外,由于某种原因,我必须用 reCAPTCHA Enterprise javascript src 链接的引号来包装我的站点密钥,以避免从 Google 收到“Bad Request 400”错误。

<script src="https://www.google.com/recaptcha/enterprise.js?render='6LfpqZcgAAAAAC7RH7qroayHutXeXkpLuKY5iV6a'"></script>
Run Code Online (Sandbox Code Playgroud)

没有任何想法来解决这些错误,我在 StackOverflow 上......

编辑 #1:看起来格式错误的令牌是由在创建 reCAPTCHA Enterprise 密钥期间启用 Web 应用程序防火墙 (WAF) 操作令牌引起的...

编辑#2:只有当您启用了复选框功能时才会发生这种情况,在新密钥上禁用它后,现在一切正常了!

最终编辑:因此,在阅读文档时,只有在页面底部我才意识到您在使用 WAF 操作令牌或会话令牌时不会创建评估。因此,仅当您启用了 WAF 操作令牌或会话令牌时才会发生此错误,因为它要求您将令牌附加到请求标头。更多详细信息请参见此处