Google Django 的 ReCAPTCHA 验证失败

Google ReCAPTCHA validation failed with Django

当输入的密钥等于验证码文本时,我一致得到以下异常:raise Recaptcha Invalid Challenge Error(challenge 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>

查看您的 RecaptchaClient 和模板脚本,您在 RecaptchaClient 中的 public 密钥似乎不正确。

这是脚本源:

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

这个link包含两个参数:

  • k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF
  • hl=en

RecaptchaClient 中的 public 键包含键后下一个参数中的“&hl”。

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