Reputation: 64
I am trying to post a buy or sell order through Poloniex's trading API, the problem is that I keep getting the following error:
{
error = "Invalid API key/secret pair.";
}
The code for posting the order is the following:
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)
}
}
I used this function to access other data from their API, of course with the right command. But this time it does not want to work.
By shuffling the parameters inside the sign
constant I would usually be able to make it work.
Their documentation is fairly poor and I have been researching this for while now. Any help is greatly appreciated.
Upvotes: 0
Views: 203