1

在看看http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html如何排序字节顺序列表AWS通话

以下名称 - 值对:

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

的名称 - 值对已按排序以字节顺序

应导致

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

如何在R中实现这一点?

据我可以告诉他们,他们排序的 as.numeric(charToRaw(name))值。如果第一个值相等,则按第二个值排序,然后按第三个排序,依此类推。

问:如何在R中做到这一点?

回答

3
# 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" 
) 

获取字节:

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

计算字节,并rbind值

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

排序第一列的名称,然后通过第二,由第三方,...和等等

names(nvp)[do.call(order, as.list(b))] 

[1] "AWSAccessKeyId" "AssociateTag" "Keywords"  "Operation"  "SearchIndex" 
[6] "Service"  "Timestamp"  "Version" 

因此最后nvp[do.call(order, as.list(b))]在正确排序的列表中返回

0

以上来自@ Floo0的答案非常好,当与来自this answer的加密签名结合使用时,它会更加有用。

我被卡住了,直到找到这两个帖子。我使用亚马逊的Signed Request Helper来验证我是否成功签署了我的查询。使用上面的代码正确规范地进行排序的查询和验证码(again found here)签字:

library(digest) 
library(RCurl) 

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

另外,我还没有使用它,但有一个Python moduleamazon-product-api,似乎少工作。

+1

也许你还应该提到这需要'digest'和'RCurl'包才能工作 – Rentrop

+0

@ Floo0,这要感谢我将它添加到代码段中。 –