Here is my example - it lets you search for music, dvds, books, etc by title (and an artist for music cds, and an author for books) and then shows the resulting images ordered into 5 different image sizes. Double click an image to open the jpg in a new window.
** As Amazon keeps updating their web service this example will stop working. Please view the source code to see how it used to work!
Here are the steps to get it working in your Flex Builder:
- Download the source code from the example above (Right click "View Source", then click the download link in the bottom left corner of the page)
- Sign up for an Amazon Web Service Account
- Copy your Access Key ID and put it in the services/AmazonItemSearch.as file
- Full details and the API for the Amazon Web Services are located here
I found this website which helped me get started:
Finding cover art with Flex and Amazon WebServices.
** August 24th, 2009 Update **
As of August 15th Amazon now requires that all requests are signed. The example above has been updated to include this change, so you can View Source to see the code.
Here are some links that I found very useful in fixing the problem:
- Getting Started - submitting your first request
- Example REST Requests (steps on how to sign the request)
- Signed Request Helper:
Use 00000000000000000000 for the access key and 1234567890 for the secret key.
Then copy the following text into the Unsigned URL textbox:http://webservices.amazon.com/onca/xml?Service=AWSECommerceServic
e&AWSAccessKeyId=00000000000000000000&Operation=ItemLookup&ItemId
=0679722769&ResponseGroup=ItemAttributes,Offers,Images,Reviews&Ve
rsion=2009-01-06&Timestamp=2009-01-01T12:00:00Z
When signing the request, I tried using the com.adobe.crypto.HMAC and com.adobe.crypto.SHA256 classes that come with the as3corelib project to create the signature, but it didn't work because the signature needs to be Base64 encoded. So instead I modified those two classes very slightly to make it work.
I also included a TestSignature.mxml application which was useful to make sure that the signature matched what the Signed Request Helper output.
8 comments:
Really liked this example.
Modifying it to also search for images using EAN / Barcodes, slightly different API call, uses ItemType=EAN and ItemId=[bar code]
Once that's done, I'm going to try and get some more meaningful information displayed, like Title, Price etc.
Thanks again for your example
Nice example..
but there're some errors on accessing AWS.. can u explain ?
Thanks.
Thanks for the comment, you are right, it was broken. As of August 15th 2009 the Amazon web services now require you to sign the requests, and I wasn't doing that.
I've update the example above and added some more information in the main blog entry.
Cheers,
Chris
Thanks again,
I've tested your updated codes(without any changes except AWSAccessKeyId and SecretKeyID) but I couldn't see any results like your demonstrated example. Maybe my codes couldn't get any results from AWS. (I've got error messages like "Can't access to Null object reference or methods..." with popup window shows original and signed request messages)
another question is, what's the difference between as2corelib's HMAC and SHA256 class and your modified classes ?
Thanks,
Hi,
That's weird that your codes don't work. I'd suggest logging into the AWS website and verifying that your codes haven't changed (my codes had changed when I logged in yesterday, no clue why).
https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=access-key
I've made comments in the HMAC and SHA256 classes to indicate what I changed from the original.
But to summarize - I changed 2 lines in HMAC, one was to default to the SHA256 algorithm (instead of MD5) and the other was the last line of the hashBytes() function to call the SHA256.hashBytesToBase64() function (instead of the hashBytes() function).
The SHA256.hashBytesToBase64() is the new function that I added. It is identical to the SHA256.hashToBase64() function except that it takes in a ByteArray instead of a String. So really the only overall change is that HMAC.hash() now returns a Base64 encoded String which is needed for the Amazon signature.
Chris,
finally, I've succeeded with modifying signing method using "Base64Encoder" and "HMAC.hash() with SHA256 method".
but, there's another problem. Sometimes my sample returns "FaultEvent" on continous trying to request. e.g it's succeed or fail with same keyword.
Help again, I couldn't solve this issue.
Thanks,
Can you tell me more about the FaultEvent that you're getting? Can you trace out the error?
e.g. trace(event.fault);
There is often some information in FaultEvent that tells you what is going wrong. Usually there is the complete URL of the request that was sent to the Amazon Web Service, and so a trick I've found is to copy that complete url (it contains all the parameters like the signature) and paste it into the addressbar of a new browser window and then you'll see the real error in XML. It's annoying how Flex's FaultEvent's hide the real problem.
I guess the other thing that could be going wrong is the request is timing out? But I doubt it, in my sample code I set the timeout to 20 seconds... (see the AmazonItemSearch.send() function).
Chris
faultCode:Channel.Security.Error faultString:'Security error accessing url' faultDetail:'Destination: DefaultHTTP'
Post a Comment