[erlang-questions] Amazon API -- Lookup by ISBN

Chandru <>
Tue Apr 14 09:29:04 CEST 2015


On 8 April 2015 at 21:47, <> wrote:

> Hello,
>
> I'm striving to look up books in Amazon's db by ISBN. At first blush it
> looks easy enough:
>
>
> http://docs.aws.amazon.com/AWSECommerceService/latest/DG/EX_LookupbyISBN.html
>
> But the last item, Signature, baffles me. Procedure here:
>
>
> http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html
>
> I'm fine with this until I hit step 4:
>
> -- Sort parameter/value pairs by byte value --- I can see how to do this
> manually, but don't know how put Erlang to the task
>
> And I'm really stumped when I hit step 8:
>
> -- Calculate an RFC 2104-compliant HMAC with the SHA256 hash algorithm
>
> Any help? Better yet, does anyone have actual code to make such requests
> they're willing to share?
>
>
Sorry, quite a late reply here, but better late than never I guess. Here is
some sample code I dug out of one of my pet projects.

sign_amazon_aws_req(Method, Params) ->
    {ok, {AWS_Access_id, Secret_key}} = application:get_env(my_app,
aws_access_credentials),

    Boiler_plate_params =
        ["Service=AWSECommerceService",
         "AWSAccessKeyId=" ++ AWS_Access_id,
         "AssociateTag=my_associate_tag",
         "Timestamp=" ++ http_uri:encode(aws_timestamp())
        ],
    Params_1       = Params ++ Boiler_plate_params,
    Req_params     = lists:flatten(string:join(lists:sort(Params_1), "&")),
    Rest_endpoint  = amazon_rest_endpoint(),
    Rest_path      = "/onca/xml",
    String_to_sign = [Method,        "\n",
                      Rest_endpoint, "\n",
                      Rest_path,     "\n",
                      Req_params],
    Signature = http_uri:encode(
                  base64:encode_to_string(
                    crypto:hmac(sha256, Secret_key, String_to_sign))),
    lists:flatten(["https://" ++ Rest_endpoint ++ Rest_path ++ "?",
                   Req_params,
                   "&Signature=", Signature]).

amazon_rest_endpoint() -> "webservices.amazon.co.uk".

aws_timestamp() ->
    {{Y, M, D}, {H, Mi, S}} = calendar:universal_time(),
    lists:flatten(io_lib:format("~p-~s-~sT~s:~s:~sZ",
                                [Y, two_digits(M), two_digits(D),
                                 two_digits(H), two_digits(Mi),
two_digits(S)])).

two_digits(X) when X < 10        -> [$0 | integer_to_list(X)];
two_digits(X) when is_integer(X) -> integer_to_list(X);
two_digits([X])                  -> [$0, X];
two_digits(X)                    -> X.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150414/5094dbf9/attachment.html>


More information about the erlang-questions mailing list