我试图通过Poloniex的交易API发布买入或卖出订单,问题是我一直收到以下错误:
{
error = "Invalid API key/secret pair.";
}
发布订单的代码如下:
func postOrder(type: String, price: String, amount: String) {
let timeNowInt = Int((NSDate().timeIntervalSince1970)*500000)
let timeNow = String(timeNowInt)
if key != "" && secret != "" {
guard let url = URL(string: "https://poloniex.com/tradingApi") else {return}
let sign = "nonce=\(timeNow)&command=\(orderType.lowercased())¤cyPair=\(coinPair)&rate=\(price)&amount=\(amount)"
let parameters: Parameters = ["command" : orderType.lowercased(), "currencyPair" : coinPair, "rate" : price, "amount" : amount, "nonce" : timeNow]
let hmacSign: String = SweetHMAC(message: sign, secret: secret).HMAC(algorithm: .sha512)
let headers: HTTPHeaders = ["key" : key, "sign" : hmacSign]
print(parameters)
print(sign)
request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON(completionHandler: {
response in
if let jsonResponse = response.result.value as? NSDictionary {
print(jsonResponse)
if let orderNumber = jsonResponse["orderNumber"] as? String {
print("placed order with id: " + orderNumber)
JSSAlertView().show(self, title: "Success", text: "The order has been placed", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .white)
self.priceTextField.text = ""
self.amountTextField.text = ""
} else {
JSSAlertView().show(self, title: "Failure", text: "Not able to place order", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .red)
}
} else {
print("json is not readable")
JSSAlertView().show(self, title: "Failure", text: "Not able to place order", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .red)
}
})
} else {
print("can't show balances because the key and secret are nil")
JSSAlertView().show(self, title: "Log in", text: "Please log in before trying to place an order", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .gray)
}
}
我使用此函数从其API访问其他数据,当然使用正确的命令。但这一次它不想工作。
通过改组sign
常量内的参数,我通常可以使其工作。
他们的文档很差,我一直在研究这个问题。任何帮助是极大的赞赏。谢谢!