Created
June 12, 2012 13:59
-
-
Save brb/2917699 to your computer and use it in GitHub Desktop.
Verifying reCAPTCHA with Erlang
This file contains 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
-module(recaptcha). | |
-export([verify/3]). | |
-define(PRIVATE_KEY, "YOU_RPRIVATE_KEY"). | |
-define(URL, "http://www.google.com/recaptcha/api/verify"). | |
%% @doc WARNING: 'inets' application should be started! | |
-spec verify(list(), list(), list()) -> ok | | |
{error, invalid_site_private_key | invalid_request_cookie | | |
incorrect_captcha_sol}. | |
verify(Challenge, Response, RemoteIp) -> | |
Params = "privatekey=" ++ ?PRIVATE_KEY ++ "&challenge=" ++ Challenge ++ | |
"&response=" ++ Response ++ "&remoteip=" ++ RemoteIp, | |
case httpc:request(post, | |
{?URL, [], "application/x-www-form-urlencoded", Params}, [], []) of | |
{ok, {_, _, "true\nsuccess"}} -> ok; | |
{ok, {_, _, Resp}} -> {error, parse_error(Resp)} | |
end. | |
parse_error("false\ninvalid-site-private-key") -> invalid_site_private_key; | |
% invalid challenge: | |
parse_error("false\ninvalid-request-cookie") -> invalid_request_cookie; | |
parse_error("false\nincorrect-captcha-sol") -> incorrect_captcha_sol. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment