Last active
December 9, 2018 17:37
-
-
Save pradumangoyal/d7e742422d16e3d7cd7b398b41f22188 to your computer and use it in GitHub Desktop.
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
... # Other imports | |
import requests | |
import get_client_ip from './utils.py' # See next snippet | |
secret_key = 'YOUR_SECRET_SITE_KEY' # Preferably from settings.py | |
class Submission(APIView): | |
... | |
def post(self, request, *args, **kwargs): | |
r = requests.post( | |
'https://www.google.com/recaptcha/api/siteverify', | |
data={ | |
'secret': secret_key, | |
'response': request.data['g-recaptcha-response'], | |
'remoteip': get_client_ip(self.request), # Optional | |
} | |
) | |
if r.json()['success']: | |
# Successfuly validated | |
# Handle the submission, with confidence! | |
return self.create(request, *args, **kwargs) | |
# Error while verifying the captcha | |
return Response(data={'error': 'ReCAPTCHA not verified.'}, status=status.HTTP_406_NOT_ACCEPTABLE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment