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

Gordon Guthrie gguthrie@REDACTED
Tue Apr 14 09:53:27 CEST 2015


There’s a pretty full clone of the Erlang AWS library in mochi that has a set of unit tests taken from the AWS documentation that you might want to use

https://github.com/mochi/mochiweb/tree/master/examples/hmac_api <https://github.com/mochi/mochiweb/tree/master/examples/hmac_api>

Beware though - the AWS canonically rewrites the URL’s a little bit before signing them so this won’t work out of the box.

G


> Le 14 avr. 2015 à 08:29, Chandru <chandrashekhar.mullaparthi@REDACTED> a écrit :
> 
> On 8 April 2015 at 21:47,  <lloyd@REDACTED <mailto:lloyd@REDACTED>> 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 <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 <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 <http://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.
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150414/797ad587/attachment.htm>


More information about the erlang-questions mailing list