我正在尝试向AWS发出API请求以获取一些产品信息。我在SO:https://stackoverflow.com/a/33084842/4208349上使用了来自此答案的快速请求签名代码。
我修改了它,以便它在我的项目中工作:
class AWSTest: UIViewController {
override func viewDidLoad() {
}
func AWSRequest(){
let operationParams: [String: String] = ["Service": "AWSECommerceService", "Operation": "ItemLookup", "ItemId": "045242127733", "IdType": "UPC", "ResponseGroup": "Images,ItemAttributes", "SearchIndex": "All"]
let keyParams = ["AWSAccessKeyId": "accesskey", "AssociateTag": "associatetag", "Timestamp": timestampFormatter.stringFromDate(NSDate())]
let fullParams = operationParams + keyParams
let signedParams = signedParametersForParameters(fullParams)
Alamofire.request(.GET, "http://webservices.amazon.com/onca/xml", parameters: signedParams).responseString { (response) in
print("Success: \(response.result.isSuccess)")
print("Response String: \(response.result.value)")
}
}
let timestampFormatter: NSDateFormatter
init() {
timestampFormatter = NSDateFormatter()
timestampFormatter.dateFormat = AWSDateISO8601DateFormat3
timestampFormatter.timeZone = NSTimeZone(name: "GMT")
timestampFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func signedParametersForParameters(parameters: [String: String]) -> [String: String] {
let sortedKeys = Array(parameters.keys).sort(<)
var components: [(String, String)] = []
for key in sortedKeys {
components += ParameterEncoding.URLEncodedInURL.queryComponents(key, parameters[key]!)
}
let query = (components.map { "\($0)=\($1)" } as [String]).joinWithSeparator("&")
let stringToSign = "GET\nwebservices.amazon.com\n/onca/xml\n\(query)"
let dataToSign = stringToSign.dataUsingEncoding(NSUTF8StringEncoding)
let signature = AWSSignatureSignerUtility.HMACSign(dataToSign, withKey: "secretKey", usingAlgorithm: UInt32(kCCHmacAlgSHA256))!
let signedParams = parameters + ["Signature": signature]
return signedParams
}
}
出于某种原因,我一直收到错误,&#34;二元运算符&#39; +&#39;不能应用于两个&#39; [String:String]&#39;操作数&#34;在这两行:
let fullParams = operationParams + keyParams
和
let signedParams = parameters + ["Signature": signature]
此外,如果我在Xcode中展开错误消息,我可以看到此消息,&#34; Overloads for&#39; +&#39;存在这些部分匹配的参数列表:(UInt8,UInt8),(Int8,Int8),(UInt16,UInt16),(Int16,Int16),(UInt32,UInt32),(Int32,Int32),(UInt64,UInt64), (Int64,Int64),(UInt,UInt),(Int,Int),(Float,Float),(Double,Double),(Float80,Float80),(String,String),(CGFloat,CGFloat),(T ,T),(C,S),(S,C),(RRC1,RRC2),(T,T.Stride),(T.Stride,T),(T,T._DisallowMixedSignArithmetic),(T._DisallowMixedSignArithmetic) ,T),(UnsafeMutablePointer,Int),(Int,UnsafeMutablePointer),(UnsafePointer,Int),(Int,UnsafePointer),(Self,Self)&#34;
任何可以帮助我找到纠正这些错误的方法都会非常感激!