python - gcloud recaptcha 企业 - 创建评估
问题描述
我正在学习 ReCaptcha Enterprise 教程,但遇到了一个400 Request contains an invalid argument
错误。我正在使用 Django。
我正在按照此处的说明https://cloud.google.com/recaptcha-enterprise/docs/create-assessment#python对 recaptcha 进行身份验证。
我已经能够用这段代码在前端加载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.
有没有办法告诉谷歌为什么生气?
我在前端使用了一个复选框,也许这意味着recaptcha_action
不需要该参数,但我不知道该怎么做。
任何帮助或指针表示赞赏!
解决方案
推荐阅读
- aws-lambda - 在 DynamoDB epoch 字段上触发 CloudWatch 事件
- python - 如何将单列数组与另一个数组相加(逐列)?
- python - 我如何知道我的应用程序支持哪些版本的依赖项?
- r - dplyr 的汇总输出是否有确定的输出顺序?
- java - 每次运行的 Google Cloud AppEngine Java 代码重复组件更新
- amazon-web-services - 删除 CloudFormation 堆栈后未取消的 Spot 请求
- python - 我可以在python3的单行代码中一次增加2个变量相同的值吗
- amazon-dynamodb - 用于查询 dynamodb 的 boto3 资源:查询条件缺少关键架构元素
- javascript - Chrome 扩展 API 方法 chrome.webrequest.onheadersreceived 未在 responseHeader 中获取内容长度
- php - 表单数据提交工作来自 POSTMAN,但不适用于 PHP 或 Curl