Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Anyone Have Perl Code for New Amazon API? [SOLVED]

by DanielSpaniel (Scribe)
on Oct 27, 2011 at 21:31 UTC ( #934250=perlquestion: print w/replies, xml ) Need Help??
DanielSpaniel has asked for the wisdom of the Perl Monks concerning the following question:

Typical of Amazon, they've recently modified their product search API (probably renamed it yet again as well knowing them) ...

They used to have sample Perl code for their API, but there is none for the new API. It's not very different from the old one, but a couple of extra parameters are required.

While this may not make much sense to anyone unfamiliar with Amazon API stuff, a typical config' (courtesy of their previous example code) has previously consisted of having two files - one a SignatureHelper file, and one a SignatureHelperInit file.

However, I'm not able to figure out how to change these files to accommodate the new API.

So, perchance there may be somebody else on here having succeeded in migrating to the new API, with Perl, using a setup similar to the signature file method, I'd be mighty grateful in knowing how to progress.

Apologies in advance for this being a bit esoteric, but having already gotten nowhere by asking on the Amazon developer site itself, I'm grasping at straws right now.

  • Comment on Anyone Have Perl Code for New Amazon API? [SOLVED]

Replies are listed 'Best First'.
Re: Anyone Have Perl Code for New Amazon API? [SOLVED]
by Your Mother (Chancellor) on Oct 28, 2011 at 03:01 UTC

      it is semi trivial to roll your own as needed with

      I highly doubt it. OTOH I'll bet it is trivial with SOAP::Simple

        Your doubt stings. I realized that XML::LibXML will now do URI fetching itself so we can leave out dealing with the user agent stuff.

        #!/usr/bin/env perl use strict; use warnings; use URI::Amazon::APA; use XML::LibXML; use XML::LibXML::XPathContext; my $key = $ENV{AWS_KEY} || "YOURKEYHERE"; my $secret = $ENV{AWS_SECRET} || "youforgottoputyourownsecretin"; my $asscociate = $ENV{AWS_ASSOC} || "this-isnt-it-20"; my $title = shift || die "You must provide a search string"; my $mode = shift || die "You must provide a mode, e.g., Books"; my $uri = URI::Amazon::APA->new(""); $uri->query_form( Service => "AWSECommerceService", Operation => "ItemSearch", ResponseGroup => "Large", AssociateTag => $asscociate, Title => $title, SearchIndex => $mode, ); $uri->sign( key => $key, secret => $secret ); my $doc = XML::LibXML->load_xml( location => $uri ); my $xc = XML::LibXML::XPathContext->new($doc); $xc->registerNs('amzn', $doc->getDocumentElement->namespaceURI); if ( my @error = $xc->findnodes('//amzn:Error') ) { die join("\n", map { $_->textContent } @error ), $/; } for my $item ( $xc->findnodes('//amzn:ItemAttributes/amzn:Title/text() +') ) { print $item->nodeValue, $/; } # print $doc->serialize(1);

        Set-up ENV or add your info to the script and then– perl Books Learning Perl Programming Perl (3rd Edition) Learning Perl, 5th Edition Perl Cookbook, Second Edition Perl Pocket Reference Regular Expression Pocket Reference: Regular Expressions for Perl, Rub +y, PHP, Python, C, Java and .NET (Pocket Reference (O'Reilly)) Intermediate Perl Modern Perl Beginning Perl for Bioinformatics Automating System Administration with Perl: Tools to Make You More Eff +icient
Re: Anyone Have Perl Code for New Amazon API?
by Anonymous Monk on Oct 27, 2011 at 21:37 UTC

      Yeah, you would think they would be pretty good on support etc, but they're really awful. There's no support from Amazon themselves (unless you pay for it! ... i.e. pay for support to fix issues so you can promote their own products) ... There is a support-like forum, but most people only visit it when they have a problem, and so there are no resident experts ... Very lucky if one can get a useful response on there.

      Anyway, thx for the links. The first one is a completely separate thing ... but the second has a grain of info' which might be useful, but it's not that great, because he only refers to changing a line number, and doesn't give any code ... and my line numbers are way different to his because I've modified the file in other ways previously.

      If anyone with additional info' then I'd still be most grateful for it

      Thanks again!

        for me as of 5 Feb 2013 it was enough to add the key AssociateTag with the Amazon-supplied value (from into my $request from which I got from and everything worked.


Re: Anyone Have Perl Code for New Amazon API?
by ww (Archbishop) on Oct 27, 2011 at 21:51 UTC
    Esoteric question; iggerant answer:

    Possibly outdated by the change you're decrying, but does Net::Amazon offer any help? Seems to have some possibilities

    ("Net::Amazon::Request::UPC  request class for UPC search")
    per the parenthesized material, quoted from CPAN). And see also with sample code of various sorts.

      :-) .. no, there are no iggerant answers in response to this question ... Anything that might help is appreciated!

      Interesting, haven't checked it yet, but I'll wander over to that sourceforge link and see if there's some sample code worth hacking ...

        Solved. This was actually much easier than what I thought it was going to be ...

        For anyone else that seeks Perl/Amazon API wisdom, for the new API, then a couple of things:

        You can't just tack on the associates ID to the end of the request URL, because: firstly the params need to be in alphabetical order (yes, really ... so odd), but most importantly, it needs to be part of the request URL which gets "signed." That is, if you just tack it in the URL some place, without it being the portion of the URL which gets signed, then it won't work ... you'll get some signature error code back from Amazon.

        So, if you have the example code from the previous API, then find the code in there which looks a bit like the code below, and simply insert the AssociateTag line in there. I think that's the only required new param, although there are several other changes to the API. The version line might need to be changed as well, although right now it works with the old version date.

        my $request = { Service => 'AWSECommerceService', AssociateTag =>'xxxxxx', Operation => 'ItemSearch', BrowseNode=> $$node, Keywords =>"$$keywords", ItemPage =>"$$itempage", SearchIndex => $$searchindex, Version => '2009-03-31', ResponseGroup => $$response, };

        Thanks to all who responded! Appreciated.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://934250]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-07-23 11:16 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (346 votes). Check out past polls.