-module(apigateway_erlang_demo).
%% API
-export([request_api/0]).
%% request_api()
request_api() ->
%% Start the inets service. If the project has already started this service, remove this parameter
inets:start(),
Url = "http://service-xxxxxxxx-1234567890.ap-beijing.apigateway.myqcloud.com/release/xxxxx",
Source = "xxxxxx",
GMTDate = now_to_utc_string(),
SecretId = "xxxDf4estwodtdzoke1234567890i3j9jv18wt9u",
SecretKey = "xxxSNF0CEp3OhN4t91234567890AWrct960X9192",
Sign = simple_sign(Source, GMTDate, SecretId, SecretKey),
Header = [
{"Source", Source},
{"x-Date", GMTDate},
{"Authorization", Sign},
{"Content-Type", "application/x-www-form-urlencoded"}
],
case httpc:request(get, {Url, Header}, [], []) of
{ok, {_StatusLine, _Header, Result}} ->
Result;
%% %% Result -> need decode
Error ->
Error
end.
simple_sign(Source, GMTDate, SecretId, SecretKey) ->
Auth = "hmac id=\\"" ++ SecretId ++ "\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date source\\", signature=\\"",
SecretKey = "xxxSNF0CEp3OhN4t91234567890AWrct960X9192",
Source = "xxxxxx",
SignStr = "x-date: " ++ GMTDate ++ "\\n" ++ "source: " ++ Source,
Mac = crypto:hmac(sha, SecretKey, SignStr),
Sign = base64:encode(Mac),
Sign2 = binary_to_list(Sign),
Sign3 = Auth ++ Sign2 ++ "\\"",
Sign3.
%% Get the current time and convert it into the format of "Mon, 02 Jan 2006 15:04:05 GMT"
now_to_utc_string() ->
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:universal_time(),
WeekNum = week_num(),
Month1 = month_to_english(Month),
WeekNum1 = week_to_english(WeekNum),
Day1 = lists:flatten(io_lib:format("~2..0w", [Day])),
Date1 = WeekNum1 ++ ", " ++ Day1 ++ " " ++ Month1,
Date2 = lists:flatten(
io_lib:format(" ~4..0w ~2..0w:~2..0w:~2..0w GMT",
[Year, Hour, Minute, Second])),
Date1 ++ Date2.
week_num() ->
{Date, _} = calendar:local_time(),
calendar:day_of_the_week(Date).
%% day_of_the_week
week_to_english(1) ->
"Mon";
week_to_english(2) ->
"Tue";
week_to_english(3) ->
"Wed";
week_to_english(4) ->
"Thu";
week_to_english(5) ->
"Fri";
week_to_english(6) ->
"Sat";
week_to_english(7) ->
"Sun".
month_to_english(1) ->
"Jan";
month_to_english(2) ->
"Feb";
month_to_english(3) ->
"Mar";
month_to_english(4) ->
"Apr";
month_to_english(5) ->
"May";
month_to_english(6) ->
"Jun";
month_to_english(7) ->
"Jul";
month_to_english(8) ->
"Aug";
month_to_english(9) ->
"Sept";
month_to_english(10) ->
"Oct";
month_to_english(11) ->
"Nov";
month_to_english(12) ->
"Dec".
Was this page helpful?