我试图通过项目搜索调用亚马逊并返回项目列表,然后我想要做的是从特定卖家返回项目。
<cfscript>
test = ItemLookup(ItemId="B00R6T0PLM",AWSAccessKeyId=request.awsaccesskeyid,AssociateTag=request.AssociateTag,secretkey=request.secretkey);
</cfscript>
<cffunction name="ItemLookup" hint="AZ lookup by PID">
<cfargument name="ItemId">
<cfargument name="AWSAccessKeyId" required="false" default="XXXXXXXXXXXXX">
<cfargument name="AssociateTag" required="false" default="zzzzz-">
<cfargument name="secretkey" required="false" default="YYYYYYYYYYYYYYYYYYY">
<cfargument name="ResponseGroup" required="false" default="Medium">
<cfset ts = '#DateFormat(now(), "YYYY-MM-DD")#T'>
<cfset tt = '#TimeFormat(now(), "HH:mm:ss")#Z'>
<cfset encts = '#ts#' & urlencodedFormat('#tt#')>
<cfset p = arraynew(1)>
<cfset p[4] = 'Service=AWSECommerceService'>
<cfset p[8] = 'AWSAccessKeyId=#arguments.AWSAccessKeyId#'>
<cfset p[3] = 'Operation=ItemLookup'>
<cfset p[2] = 'ItemId=#arguments.ItemId#'>
<cfset p[5] = 'Timestamp=#encts#'>
<cfset p[6] = 'AssociateTag=#arguments.AssociateTag#'>
<cfset p[7] = 'Version=2011-08-01'>
<cfset p[1] = 'ResponseGroup=#arguments.ResponseGroup#'>
<cfset ArraySort(p,"text","asc")>
<cfset req_params = '#p[1]#&#p[2]#&#p[3]#&#p[4]#&#p[5]#&#p[6]#&#p[7]#&#p[8]#'>
<cfset lineBreak = Chr(10)>
<cfset tosign = 'GET#lineBreak#webservices.amazon.in#lineBreak#/onca/xml#lineBreak##req_params#'>
<cfset signed = URLEncodedFormat(ToBase64(HMAC_SHA256(tosign,"#arguments.secretkey#")))>
<cfset req = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemSearch&AWSAccessKeyId=arguments.AWSAccessKeyId&AssociateTag=arguments.AssociateTag&SearchIndex=Apparel&Keywords=Shirt&Timestamp=#ts##tt#&Signature=#signed#'>
<cfhttp url='#req#' method="get" result="res"/>
<cfreturn res.filecontent>
<cfreturn tosign>
</cffunction>
<cffunction name="HMAC_SHA256" returntype="binary" access="public" output="no" hint="Sign for AZ request">
<cfargument name="signMessage" type="string" required="true" />
<cfargument name="signKey" type="string" required="true" />
<cfset local.jMsg = JavaCast("string",arguments.signMessage).getBytes("iso-8859-1") />
<cfset local.jKey = JavaCast("string",arguments.signKey).getBytes("iso-8859-1") />
<cfset local.key = createObject("java","javax.crypto.spec.SecretKeySpec") />
<cfset local.mac = createObject("java","javax.crypto.Mac") />
<cfset local.key = local.key.init(local.jKey,"HmacSHA256") />
<cfset local.mac = local.mac.getInstance(local.key.getAlgorithm()) />
<cfset local.mac.init(local.key) />
<cfset local.mac.update(local.jMsg) />
<cfreturn local.mac.doFinal() />
</cffunction>
<cfdump var="#test#">
我要回的错误是......
<?xml version="1.0"?> <ItemSearchErrorResponse xmlns="http://ecs.amazonaws.com/doc/2005-10-05/"><Error><Code>RequestExpired</Code><Message>Request has expired. Timestamp date is 2016-08-09T11:42:19Z.</Message></Error><RequestId>c5852914-b6a1-474a-9977-28a20dcb5029</RequestId></ItemSearchErrorResponse>
答案 0 :(得分:3)
error message可能意味着它的内容。时间戳值太旧了:
RequestExpired - 请求在日期后超过15分钟到达服务 请求或在请求后超过15分钟盖章 到期日期(例如预签名的URL)或日期戳 该请求将来超过15分钟。如果你正在使用 临时安全凭证,如果出现此错误 凭证已过期。
这可能是由于时区造成的。由于您的时间戳字符串使用 Z ,it will be considered UTC time。如果JVM的实际时区不同,您可能会提交已经过去或过去太远的日期和时间(至少在UTC中)。首先尝试将now()
转换为UTC:
<cfset utcNow = dateConvert("local2utc", now())>
<cfset ts = DateFormat(utcNow , "YYYY-MM-DD")&"T">
<cfset tt = TimeFormat(utcNow , "HH:mm:ss")&"Z">
答案 1 :(得分:0)
我认为使用Now()作为时间戳的基础意味着请求在发送时已经过期。
尝试向Now()值添加30分钟:
<cfset ts = '#DateFormat(DateAdd("n", 30, Now()), "YYYY-MM-DD")#T'>
<cfset tt = '#TimeFormat(DateAdd("n", 30, Now()), "HH:mm:ss")#Z'>