gcloud recaptcha 企业 - 创建评估

问题描述

我正在学习 ReCaptcha Enterprise 教程,但遇到了 400 Request contains an invalid argument 错误。我正在使用 Django。

我正在按照 https://cloud.google.com/recaptcha-enterprise/docs/create-assessment#python 此处的说明对重新验证码进行身份验证。

我已经能够使用此代码在前端加载 recaptcha

{% block content %}
<script type="text/javascript">
  var onloadCallback = function() {
    grecaptcha.enterprise.render('html_element',{
      'sitekey' : '{{ sitekey }}',});
  };
</script>
<form action="{% url 'captcha-authentication' %}" method="post">
  {% csrf_token %}
  <div id="html_element"></div>
  <input type="submit" value="Submit">
</form>

<script src="https://www.google.com/recaptcha/enterprise.js?onload=onloadCallback&render=explicit"
async defer>
</script>

我已确认 request.POST 包含“g-recaptcha-response”键。对于我的 site_key,我使用来自 https://console.cloud.google.com/security/recaptcha 的密钥 ID。

def confirm_captcha(token,site_key,recaptcha_action):
  parent_project = "t-commerce-321516"

  client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

  event = recaptchaenterprise_v1.Event()
  event.site_key = site_key
  event.token = token
  event.expected_action = recaptcha_action

  assessment = recaptchaenterprise_v1.Assessment()
  assessment.event = event

  request = recaptchaenterprise_v1.CreateAssessmentRequest()
  request.assessment = assessment
  request.parent = parent_project

  # this is the line that is throwing an error
  response = client.create_assessment(request)

  if not response.token_properties.valid:
    print("The CreateAssessment() call Failed because the token was " +
          "invalid for the following reasons: "
          + str(response.token_properties.invalid_reason))
  else:
    if response.event.expected_action == recaptcha_action:
      print("The reCAPTCHA score for this token is: " +
            str(response.risk_analysis.score))
      return True
    else:
      print("The action attribute in your reCAPTCHA tag does" +
            "not match the action you are expecting to score")

  return False

创建客户评估的错误400 Request contains an invalid argument.

有什么方法可以说明 Google 生气的原因吗?

我在前端使用了一个复选框,这可能意味着不需要 recaptcha_action 参数,但我不知道该怎么做。

感谢任何帮助或指示!

解决方法

我认为你需要:

request.parent = `projects/${parent_project}`

每个:CreateAssessmentRequest

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...