· 6 years ago · Oct 04, 2019, 06:56 AM
1<!--- this is a sample Adobe ColdFusion script for sending API request to SellerCenter --->
2<!--- hashing function --->
3<cffunction name="HMAC_SHA256" returntype="string" access="private" output="false">
4 <cfargument name="Data" type="string" required="true" />
5 <cfargument name="Key" type="string" required="true" />
6 <cfargument name="Bits" type="numeric" required="false" default="256" />
7 <cfset var i = 0 />
8 <cfset var HexData = "" />
9 <cfset var HexKey = "" />
10 <cfset var KeyLen = 0 />
11 <cfset var KeyI = "" />
12 <cfset var KeyO = "" />
13 <cfset HexData = BinaryEncode(CharsetDecode(Arguments.data, "iso-8859-1"), "hex") />
14 <cfset HexKey = BinaryEncode(CharsetDecode(Arguments.key, "iso-8859-1"), "hex") />
15 <cfset KeyLen = Len(HexKey)/2 />
16 <cfif KeyLen gt 64>
17 <cfset HexKey = Hash(CharsetEncode(BinaryDecode(HexKey, "hex"), "iso-8859-1"), "SHA-256", "iso-8859-1") />
18 <cfset KeyLen = Len(HexKey)/2 />
19 </cfif>
20 <cfloop index="i" from="1" to="#KeyLen#">
21 <cfset KeyI = KeyI & Right("0"&FormatBaseN(BitXor(InputBaseN(Mid(HexKey,2*i-
22 1,2),16),InputBaseN("36",16)),16),2) />
23 <cfset KeyO = KeyO & Right("0"&FormatBaseN(BitXor(InputBaseN(Mid(HexKey,2*i-
24 1,2),16),InputBaseN("5c",16)),16),2) />
25 </cfloop>
26 <cfset KeyI = KeyI & RepeatString("36",64-KeyLen) />
27 <cfset KeyO = KeyO & RepeatString("5c",64-KeyLen) />
28 <cfset HexKey = Hash(CharsetEncode(BinaryDecode(KeyI&HexData, "hex"), "iso-8859-1"), "SHA-256", "iso-8859-1")
29 />
30 <cfset HexKey = Hash(CharsetEncode(BinaryDecode(KeyO&HexKey, "hex"), "iso-8859-1"), "SHA-256", "iso-8859-
31 1") />
32 <cfreturn Left(HexKey,arguments.Bits/4) />
33</cffunction>
34<!---/ hashing function --->
35<!--- define --->
36<cfset send_xml = false><!--- for APIs that need XML request body, like xxxxProduct APIs --->
37 <cfset secret_key="562aeae4090d3a62ef171b6646cc2bdac6417473"/>
38 <cfset sc_api_host="http://sellercenter-api.local/"/>
39 <!---/ define --->
40 <!--- request params --->
41 <cfset params = structNew() />
42 <cfset params["example@example.com"] = "UserID"/>
43 <cfset params["GetShipmentProviders"] = "Action"/>
44 <cfset params["2014-07-24T20:06:33+02:00"] = "Timestamp"/>
45 <cfset params["1.0"] = "Version"/>
46 <cfsavecontent variable="strXML">
47 <?xml version="1.0" encoding="UTF-8" ?>
48 <Request>
49 <Product>
50 <SellerSku>4105382173aaee4</SellerSku>
51 <Price>12</Price>
52 </Product>
53 </Request>
54 </cfsavecontent>
55 <!---/ request params --->
56 <!--- generate signature --->
57 <cfset strtohash = "" />
58 <cfloop list="#ArrayToList(StructSort(params, "text", "asc"))#" index="key" >
59 <cfset strtohash = strtohash & #params[key]# & "=" & #URLEncodedFormat(key)# & "&" />
60 </cfloop>
61 <cfset strtohash = #RemoveChars(strtohash, len(strtohash), 1)#/>
62 <cfset strtohash = #Replace(strtohash, "%2D", "-", "All")#/>
63 <cfset strtohash = #Replace(strtohash, "%2E", ".", "All")#/>
64 <cfset signature="#LCase(HMAC_SHA256(strtohash, secret_key))#"/>
65 <!---/ generate signature --->
66 <!--- issue the request --->
67 <cfif send_xml>
68 <cfset http_method = "post" />
69 <cfelse>
70 <cfset http_method = "get" />
71 </cfif>
72 <cfhttp method="#http_method#" url="#sc_api_host#">
73 <cfloop list="#ArrayToList( StructSort(params, "text", "asc") )#" index="key" >
74 <cfhttpparam type="url" name="#params[key]#" value="#key#"></cfhttpparam>
75 </cfloop>
76 <cfhttpparam type="xml" value="#strXML.Trim()#"></cfhttpparam>
77 <cfhttpparam type="url" name="Signature" value="#signature#"></cfhttpparam>
78 </cfhttp>
79 <!---/ issue the request --->
80 <!--- XML response --->
81 <cfoutput>#cfhttp.filecontent#</cfoutput>