DATA Step, Macro, Functions and more

Connecting to amazon web service

Reply
Contributor
Posts: 24

Connecting to amazon web service

Hi,

I am trying to connect ot amazon web service form SAS but I can't find it how to generate the signature parameter. In their documentation, they say we need this HMAC-SHA256 parameter in the query. Do you have any code to generate it?

http://docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/index.html?APPNDX_GeneratingaSig...

Thanks a lot

Romain

Trusted Advisor
Posts: 1,300

Connecting to amazon web service

What are you trying to make this call from?  What SAS software do you license?  What OS is this running on?

Contributor
Posts: 24

Connecting to amazon web service

I am just trying to play with the web service. I found this code and I believe it is not working because amazon added the signature parameter after this paper was released. I am using SAS 9.2 on win 7

filename amazon url "http://ecs.amazonaws.com/onca/xml?

Service=AWSECommerceService&

AWSAccessKeyId= [Access Key ID]&

Operation=ItemLookup&

ItemId=B00008OE6I&

IdType=ASIN&"

lrecl=2000

recfm=S

;

data _null_ ;

infile amazon truncover;

input;

put _infile_;

run;

Trusted Advisor
Posts: 1,300

Connecting to amazon web service

In order to generate a key in SAS that uses sha256 or sha1 you need to license SAS/Secure software.  There are ways around this by using other programs.  The link you provided is for the FPS (Flexible Payments Service), which is not what you are actually trying to utilize based on the code above.  You are trying to interact with the AWSECommerceService.

This is the proper documentation for that API you are attempting to interact with:

http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?RequestAuthenticationArti...

You should make sure you are using for proper secret keys first: http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?ViewingCredentials.html

The process you are missing is outlined here: http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?rest-signature.html

You need to outsource this key generation to a different technology, for instance, perl...

Download perl here: http://www.activestate.com/activeperl/downloads

Now you need to install the Digest:Smiley FrustratedHA:Smiley TongueurePerl module (There is another version that is faster but requires C/C++ compilers, this one should present you with fewer problems, unless you know what you are doing). 

In a command prompt window you want to type ppm install 'Digest:Smiley FrustratedHA:Smiley TongueurePerl'

If you encounter problems try this site: http://www.activestate.com/blog/2010/10/how-install-cpan-modules-activeperl

Now that you have perl and the digest module installed you will want to create a perl script to call from SAS that will return the key you want.  It should look something like this: (note everything below is written quickly and not tested, at all)

#! usr/bin/perl

use FileHandle;

open INFILE, '<', $ARGV[0] or die "error opening $ARGV[0]: $!";

my $data = do { local $/; <INFILE> };

use Digest:Smiley FrustratedHA:Smiley TongueurePerl qw(hmac_sha256_base64);

$digest=hmac_sha256_base64($data, $ARGV[1]);

print $digest;

close INFILE;

From SAS you would want to call this:

data _null_;

  file 'C:\mytempfile' lrecl=5000 termstr=cr;

  put 'GET';

  put 'ecs.amazonaws.com';

  put '/onca/xml';

  put 'AWSAccessKeyId=00000000000000000000&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReviews&Service=AWSECommerceService&Timestamp=20090101T12%3A00%3A00Z&Version=2009-01-06';

run;

filename key pipe 'C:\path\to\perlscript.pl C:\mytempfile myaws_secretkey';

data foo;

  infile key;

  input key $;

run;

Now you have what you need to build the full url including the HMAC-SHA Signature...  Of course a full and proper implementation of this will be much more complex.

Contributor
Posts: 24

Connecting to amazon web service

Thanks a lot for this very detailled explanation!

Ask a Question
Discussion stats
  • 4 replies
  • 485 views
  • 4 likes
  • 2 in conversation