1. Requirement
OTP/15B
oauth.erl : https://github.com/tim/erlang-oauth.git
2. Run
application:start(inets),
application:start(crypto),
application:start(public_key),
application:start(ssl).
3. Code
-define(HOST,"https://www.google.com/accounts/ClientLogin").
-define(C2DM_HOST,"https://android.apis.google.com/c2dm/send").
-define(ACCOUNT_TYPE, "GOOGLE").
-define(SERVICE, "ac2dm").
send(RegistrationId, Msg, Title, Auth) ->
RequestBody = oauth:uri_params_encode([
{"registration_id", RegistrationId},
{"collapse_key", 1},
{"delay_while_idle", 1},
{"data.title", Title},
{"data.msg", Msg}]),
Request = {?C2DM_HOST, [{"Authorization","GoogleLogin auth=" ++ Auth}],
"application/x-www-form-urlencoded", RequestBody},
%{ok, {{_,200,_},_Header, RespBody}} = httpc:request(post, Request, [{ssl, [{verify,0}]}], []),
io:format("c2dm result : ~p~n", [httpc:request(post, Request, [{ssl, [{verify,0}]}], [])]).
send(RegistrationId, Msg, Title, Email, Passwd, Source) ->
AuthParams = get_authorize(Email, Passwd, Source),
Auth = proplists:get_value("Auth", AuthParams),
send(RegistrationId, Msg, Title, Auth).
get_authorize(Email, Passwd, Source)->
RequestBody = oauth:uri_params_encode([
{"Email", Email},
{"Passwd", Passwd},
{"accountType", ?ACCOUNT_TYPE},
{"source", Source},
{"service", ?SERVICE}]),
Request = {?HOST, [], "application/x-www-form-urlencoded", RequestBody},
{ok, {{_,200,_},_Header, ResponseBody}} = httpc:request(post, Request, [{ssl, [{verify,0}]}], []),
AuthParams = string:tokens(ResponseBody, "\n"),
oauth:uri_params_decode(string:join(AuthParams, "&")).