2
votes

Today i have been unable to make a request for an "ItemSearch" request for the product advertising api for aws.

First off, this was working Monday Feburary 24th and now March 1st it does not work. I don't know if there was any updates that may have changed the way AWS works. I couldn't find anything when searching.

My first error is: "Request has expired. Timestamp date is 140301235753Z" this means that the time i enter for the request "140301235753Z" is 15 minutes out of range of what AWS has stored for the UTC time. I do a bit of research and end up changing the code below.

NSDateFormatter *UTCFormatter = [[NSDateFormatter alloc] init];
UTCFormatter.dateFormat = @"yyMMddHHmmss'Z'";
UTCFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSString *timeStamp = [UTCFormatter stringFromDate:[NSDate date]];

i changed the @"yyMMddHHmmss'Z'" to @"yyyy-MM-dd'T'HH:mm:ss'Z'"

Re-running the request again i came across this error: "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method." this means that the signature portion of the API call that i calculate is wrong when comparing the one AWS calculates when it receives my request.

I have used this link Amazon Signature Examples as a reference to check my code below.

// create HMAC with SHA256
const char *cKey  = [secretKey cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [canonicalString cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

NSData *hashData = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH];
NSString *signature = [[DTBase64Coding stringByEncodingData:hashData] stringByURLEncoding];

I don't see an error with my code. I have looked, via google, for an application that may be able to create the string i desire in order to test if my signature is correct, but AWS didn't seem to have anything (that i could find, i could just be a bad googler).

My secret key for the AWS had a "/" character in it. I thought this might be messing with the algorithms for calculating the HMAC. So i created new secret keys until amazon generated me one with out odd characters and tested it. It did not work....

Ultimately what i am trying to make is an AWS Product Advertising API Request using this information

NSString *verb = @"GET";
NSString *hostName = @"webservices.amazon.com";
NSString *path = @"/onca/xml";

NSDictionary *params = @{
                         @"Service": @"AWSECommerceService",
                         @"AWSAccessKeyId": accessKey,
                         @"Operation": @"ItemSearch",
                         @"ResponseGroup": @"Large",
                         @"SearchIndex": @"Books",
                         @"Title": bookTitle,
                         @"AssociateTag" : trackingID
                         };

I have substituted sensitive/dynamic information with variables.

I have searched through the documentation PDFs at AWS Product Advertising Documentation and can't seem to figure out my problem.

The most baffling part is that my code was working 6 days ago and i have changed nothing.

2
hey A'sa, did you find the solutions of your problem, I'm facing the same issue...BADRI
I made a noobie mistake and commented out the code that encoded the body parameters. If you are having a similar issues i would verify that your url is correct and that the body has the correct parameters :3A'sa Dickens

2 Answers

0
votes

I've had a similar experience with another third party API. Apparently my request was being redirected and I had to resign my request.

Be sure to sign your request in this nsurlrequest delegate method:

- (NSURLRequest *)connection: (NSURLConnection *)inConnection
             willSendRequest: (NSURLRequest *)inRequest
            redirectResponse: (NSURLResponse *)inRedirectResponse;
0
votes

I made a noob mistake and commented out the code that encoded the body parameters. After uncommenting it and setting the values it all worked fine. So make sure you encode your parameters.