Skip to content

Instantly share code, notes, and snippets.

@brb
Created June 12, 2012 13:59
Show Gist options
  • Save brb/2917699 to your computer and use it in GitHub Desktop.
Save brb/2917699 to your computer and use it in GitHub Desktop.
Verifying reCAPTCHA with Erlang
-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