Created
December 19, 2013 19:45
-
-
Save earthtrip/8045030 to your computer and use it in GitHub Desktop.
Simple Recaptcha validation method when using Google App Engine (GAE), Webapp2 and WTForms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate_captcha(self, form): | |
recaptcha_challenge_field = self.request.get("recaptcha_challenge_field") | |
recaptcha_response_field = self.request.get("recaptcha_response_field") | |
remote_ip = self.request.remote_addr | |
recaptcha_private_key = 'MY_PRIVATE_KEY' # put recaptcha private key here | |
recaptcha_post_data = {"privatekey": recaptcha_private_key, | |
"remoteip": remote_ip, | |
"challenge": recaptcha_challenge_field, | |
"response": recaptcha_response_field} | |
response = urlfetch.fetch(url='http://www.google.com/recaptcha/api/verify', payload=urlencode(recaptcha_post_data), method="POST") | |
captcha_ok = True if response.content.split("\n")[0] == "true" else False | |
if not captcha_ok: | |
form.captcha.errors.append("Invalid captcha values, please try again") | |
return captcha_ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The HTML fragment that generates the form fields needed for this to work is here:
https://gist.github.com/mengelhart/8045070