How to sort list by bytes for AWS calls

Looking at http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

The following pairs of name values:

Service=AWSECommerceService Version=2011-08-01 AssociateTag=PutYourAssociateTagHere Operation=ItemSearch SearchIndex=Books Keywords=harry+potter Timestamp=2015-09-26T14:10:56.000Z AWSAccessKeyId=123 

Name-value pairs were sorted by byte

It should turn out

 AWSAccessKeyId=123 AssociateTag=PutYourAssociateTagHere Keywords=harry%20potter Operation=ItemSearch SearchIndex=Books Service=AWSECommerceService Timestamp=2015-09-26T14%3A10%3A56.000Z Version=2011-08-01 

How to achieve this in R?

As far as I can tell, they are sorted by their as.numeric(charToRaw(name)) . If the first value is equal, then they are sorted by the second, then the third, etc.

Question: how to do this in R?

+1
sorting r endianness amazon-web-services amazon-product-api
source share
2 answers
 # Name-Value-Pairs nvp <- list( "Service"="AWSECommerceService", "Version"="2011-08-01", "AssociateTag"="PutYourAssociateTagHere", "Operation"="ItemSearch", "SearchIndex"="Books", "Keywords"="harry potter", "Timestamp"="2015-09-26T14:10:56.000Z", "AWSAccessKeyId"="123" ) 

Receive byte:

 bytes <- function(chr){ as.data.frame(t(as.numeric(charToRaw(chr)))) } 

Calculate bytes and change values

 b <- lapply(names(nvp), bytes) b <- data.table::rbindlist(b, fill=TRUE) # other than base::rbind, this fills by NA 

Order the names in the first column, then in the second, third, ... etc.

 names(nvp)[do.call(order, as.list(b))] [1] "AWSAccessKeyId" "AssociateTag" "Keywords" "Operation" "SearchIndex" [6] "Service" "Timestamp" "Version" 

So finally nvp[do.call(order, as.list(b))] returns in a properly sorted list

+3
source share

The answer above from @ Floo0 is very good and even more useful when combined with an encrypted signature from this answer .

I got stuck until I found these two posts. I used the Amazon Signed Request Assistant to ensure that I successfully signed my request. Use the code above to correctly sort the query canonically and this code ( found again here ) to sign it:

 library(digest) library(RCurl) curlEscape( base64( hmac(enc2utf8((secret_key)), enc2utf8(string_to_sign), algo = 'sha256', serialize = FALSE, raw = TRUE) ) ) 

Also, I haven't used it yet, but there is a Python module , amazon-product-api , which seems to work less.

0
source share

All Articles