使用Django进行Google ReCAPTCHA验证失败

时间:2016-09-07 15:43:47

标签: django recaptcha

当输入的密钥等于验证码文本时,我一致得到以下异常:提高Recaptcha无效挑战错误(挑战ID)。 这是我的后端脚本验证:

recaptcha_client = RecaptchaClient('6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe', '6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF&hl')
try:
    is_solution_correct = recaptcha_client.is_solution_correct(
                          str(request.POST["recaptcha_response_field"]),
                          str(request.POST["recaptcha_challenge_field"]),
                          '127.0.0.1',
                           )
    except RecaptchaUnreachableError as exc:
                                           print exc.message
                                           except RecaptchaException as exc:
                                                            print exc.message
                                        else:
                                             if is_solution_correct:
                                                  print('it works')
                                              else:
                                                  print('captcha error')

这是为验证码输入保留的模板脚本的一部分:

<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF&hl=en"></script>
<noscript>
     <iframe src="https://www.google.com/recaptcha/api/noscript?k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF&hl=fr" height="300" width="500" frameborder="0"></iframe><br />
     <textarea   name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
     <input type='hidden'   name='recaptcha_response_field' value='manual_challenge' />
</noscript>

1 个答案:

答案 0 :(得分:2)

查看RecaptchaClient和模板脚本,看来RecaptchaClient中的公钥不正确。

以下是脚本来源:

https://www.google.com/recaptcha/api/challenge?k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF&hl=en

此链接包含两个参数:

  • k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF
  • hl=en

RecaptchaClient中的公钥包含密钥后面的下一个参数中的“&amp; hl”。

recaptcha_client = RecaptchaClient('6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe', '6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF')