首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gcloud recaptcha企业版-创建评估

gcloud recaptcha企业版-创建评估
EN

Stack Overflow用户
提问于 2021-07-31 20:31:48
回答 1查看 419关注 0票数 0

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

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

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

代码语言:javascript
复制
{% 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。

代码语言:javascript
复制
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.

有没有办法说出谷歌为什么会生气?

我在前端使用了一个复选框,这可能意味着不需要recaptcha_action参数,但我不确定如何做到这一点。

如有任何帮助或建议,我们将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-02 15:26:12

我认为你需要:

代码语言:javascript
复制
request.parent = `projects/${parent_project}`

per:CreateAssessmentRequest

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68605666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档