Skip to content

Instantly share code, notes, and snippets.

@earthtrip
Created December 19, 2013 19:45
Show Gist options
  • Save earthtrip/8045030 to your computer and use it in GitHub Desktop.
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.
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
@earthtrip
Copy link
Author

The HTML fragment that generates the form fields needed for this to work is here:

https://gist.github.com/mengelhart/8045070

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment